<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">
<script>
    (function () {
        if ('') {
            if (prompt('请输入文章密码') !== '') {
                alert('密码错误！');
                if (history.length === 1) {
                    location.replace("https://google.com"); // 这里替换成你的首页
                } else {
                    history.back();
                }
            }
        }
    })();
</script>








<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta name="description" content="Laravel 网站不会被嵌入到其他站点中123456789101112131415161718192021222324252627282930313233343536373839404142434445HTTP 响应头部中，有一个字段，叫做 X-Frame-Options，该字段可以用来指示是否允许自己的网站被嵌入到其他网站的 &amp;lt;iframe&amp;gt; 或者 &amp;lt;object&amp;gt; 标">
<meta property="og:type" content="article">
<meta property="og:title" content="laravel 记录">
<meta property="og:url" content="http://yoursite.com/2019/10/16/laravel-记录/index.html">
<meta property="og:site_name" content="苏生不惑的博客">
<meta property="og:description" content="Laravel 网站不会被嵌入到其他站点中123456789101112131415161718192021222324252627282930313233343536373839404142434445HTTP 响应头部中，有一个字段，叫做 X-Frame-Options，该字段可以用来指示是否允许自己的网站被嵌入到其他网站的 &amp;lt;iframe&amp;gt; 或者 &amp;lt;object&amp;gt; 标">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2020-01-20T07:21:45.872Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="laravel 记录">
<meta name="twitter:description" content="Laravel 网站不会被嵌入到其他站点中123456789101112131415161718192021222324252627282930313233343536373839404142434445HTTP 响应头部中，有一个字段，叫做 X-Frame-Options，该字段可以用来指示是否允许自己的网站被嵌入到其他网站的 &amp;lt;iframe&amp;gt; 或者 &amp;lt;object&amp;gt; 标">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/10/16/laravel-记录/">



<meta name="referrer" content="never"> ​​​​


  <title>laravel 记录 | 苏生不惑的博客</title>
  








</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">苏生不惑的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/10/16/laravel-记录/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏生不惑">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏生不惑的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">laravel 记录</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-10-16T19:45:15+08:00">
                2019-10-16
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv"><i class="fa fa-file-o"></i>
            <span class="busuanzi-value" id="busuanzi_value_page_pv"></span>次
            </span>
          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  17.1k 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  85 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="Laravel-网站不会被嵌入到其他站点中"><a href="#Laravel-网站不会被嵌入到其他站点中" class="headerlink" title="Laravel 网站不会被嵌入到其他站点中"></a>Laravel 网站不会被嵌入到其他站点中</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">HTTP 响应头部中，有一个字段，叫做 X-Frame-Options，该字段可以用来指示是否允许自己的网站被嵌入到其他网站的 &lt;iframe&gt; 或者 &lt;object&gt; 标签中。该头部有三个值</span><br><span class="line"></span><br><span class="line">DENY - 始终不允许嵌入，即使是同一个域名</span><br><span class="line">SAMEORIGIN - 只能在相同域名中嵌入</span><br><span class="line">ALLOW-FROM uri - 设置允许的域</span><br><span class="line">通常，可以在 HTTP 代理中进行配置，比如 nginx</span><br><span class="line"></span><br><span class="line">add_header X-Frame-Options SAMEORIGIN;</span><br><span class="line">Laravel 自带了用来「只允许同域名嵌入」的中间件，我们只需要在 /app/Http/Kernel.php 中添加即可</span><br><span class="line"></span><br><span class="line"><span class="comment">// /app/Http/Kernel.php</span></span><br><span class="line">protected $middleware = [</span><br><span class="line">    \Illuminate\Http\Middleware\FrameGuard::<span class="class"><span class="keyword">class</span>,</span></span><br><span class="line">];</span><br><span class="line">该中间件的实现如下</span><br><span class="line"></span><br><span class="line">&lt;?php</span><br><span class="line"></span><br><span class="line">namespace Illuminate\Http\Middleware;</span><br><span class="line"></span><br><span class="line">use Closure;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">FrameGuard</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Handle the given request and get the response.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @param  \Illuminate\Http\Request  $request</span></span><br><span class="line"><span class="comment">     * @param  \Closure  $next</span></span><br><span class="line"><span class="comment">     * @return \Symfony\Component\HttpFoundation\Response</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">handle</span>(<span class="params">$request, Closure $next</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $response = $next($request);</span><br><span class="line"></span><br><span class="line">        $response-&gt;headers-&gt;set(<span class="string">'X-Frame-Options'</span>, <span class="string">'SAMEORIGIN'</span>, <span class="literal">false</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $response;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/35201</span></span><br><span class="line">https:<span class="comment">//securityheaders.com/</span></span><br><span class="line"></span><br><span class="line">可以直接扫描特定 URL 是否包含一些安全头信息。类似的头还有 X-Content-Type-Options、 Referrer-Policy、Feature-Policy 等。</span><br></pre></td></tr></table></figure>
<h3 id="Uncaught-ReflectionException-Class-request-does-not-exist"><a href="#Uncaught-ReflectionException-Class-request-does-not-exist" class="headerlink" title="Uncaught ReflectionException: Class request does not exist"></a>Uncaught ReflectionException: Class request does not exist</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">PHP Fatal error:  Uncaught ReflectionException: Class request does not exist <span class="keyword">in</span> /home/vagrant/learnku/vendor/laravel/framework/src/Illuminate/Container/Container.php:<span class="number">790</span></span><br><span class="line">Stack trace:</span><br><span class="line">在 App\Exceptions\Handler::report() 方法里，使用：</span><br><span class="line"></span><br><span class="line">dd($exception);</span><br></pre></td></tr></table></figure>
<h3 id="Laravel-下-TNTSearch-jieba-PHP-实现中文全文搜索"><a href="#Laravel-下-TNTSearch-jieba-PHP-实现中文全文搜索" class="headerlink" title="Laravel 下 TNTSearch+jieba-PHP 实现中文全文搜索"></a>Laravel 下 TNTSearch+jieba-PHP 实现中文全文搜索</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br></pre></td><td class="code"><pre><span class="line">TNTSearch+jieba-php 这套组合可以在不依赖第三方的情况下实现中文全文搜索；</span><br><span class="line">composer <span class="built_in">require</span> vanry/laravel-scout-tntsearch</span><br><span class="line"><span class="string">'providers'</span> =&gt; [</span><br><span class="line">    ...</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * TNTSearch 全文搜索</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    Laravel\Scout\ScoutServiceProvider::<span class="class"><span class="keyword">class</span>,</span></span><br><span class="line">    Vanry\Scout\TNTSearchScoutServiceProvider::class,</span><br><span class="line">],</span><br><span class="line">composer <span class="built_in">require</span> fukuball/jieba-php</span><br><span class="line"></span><br><span class="line">php artisan vendor:publish --provider=<span class="string">"Laravel\Scout\ScoutServiceProvider"</span></span><br><span class="line"></span><br><span class="line">配置项 config/scout.php 中增加 tntsearch</span><br><span class="line"></span><br><span class="line"><span class="string">'tntsearch'</span> =&gt; [</span><br><span class="line">    <span class="string">'storage'</span> =&gt; storage_path(<span class="string">'indexes'</span>), <span class="comment">//必须有可写权限</span></span><br><span class="line">    <span class="string">'fuzziness'</span> =&gt; env(<span class="string">'TNTSEARCH_FUZZINESS'</span>, <span class="literal">false</span>),</span><br><span class="line">    <span class="string">'searchBoolean'</span> =&gt; env(<span class="string">'TNTSEARCH_BOOLEAN'</span>, <span class="literal">false</span>),</span><br><span class="line">    <span class="string">'asYouType'</span> =&gt; <span class="literal">false</span>,</span><br><span class="line"></span><br><span class="line">    <span class="string">'fuzzy'</span> =&gt; [</span><br><span class="line">        <span class="string">'prefix_length'</span> =&gt; <span class="number">2</span>,</span><br><span class="line">        <span class="string">'max_expansions'</span> =&gt; <span class="number">50</span>,</span><br><span class="line">        <span class="string">'distance'</span> =&gt; <span class="number">2</span>,</span><br><span class="line">    ],</span><br><span class="line"></span><br><span class="line">    <span class="string">'tokenizer'</span> =&gt; [</span><br><span class="line">        <span class="string">'driver'</span> =&gt; env(<span class="string">'TNTSEARCH_TOKENIZER'</span>, <span class="string">'default'</span>),</span><br><span class="line"></span><br><span class="line">        <span class="string">'jieba'</span> =&gt; [</span><br><span class="line">            <span class="string">'dict'</span> =&gt; <span class="string">'small'</span>,</span><br><span class="line">            <span class="comment">//'user_dict' =&gt; resource_path('dicts/mydict.txt'), //自定义词典路径</span></span><br><span class="line">        ],</span><br><span class="line"></span><br><span class="line">        <span class="string">'analysis'</span> =&gt; [</span><br><span class="line">            <span class="string">'result_type'</span> =&gt; <span class="number">2</span>,</span><br><span class="line">            <span class="string">'unit_word'</span> =&gt; <span class="literal">true</span>,</span><br><span class="line">            <span class="string">'differ_max'</span> =&gt; <span class="literal">true</span>,</span><br><span class="line">        ],</span><br><span class="line"></span><br><span class="line">        <span class="string">'scws'</span> =&gt; [</span><br><span class="line">            <span class="string">'charset'</span> =&gt; <span class="string">'utf-8'</span>,</span><br><span class="line">            <span class="string">'dict'</span> =&gt; <span class="string">'/usr/local/scws/etc/dict.utf8.xdb'</span>,</span><br><span class="line">            <span class="string">'rule'</span> =&gt; <span class="string">'/usr/local/scws/etc/rules.utf8.ini'</span>,</span><br><span class="line">            <span class="string">'multi'</span> =&gt; <span class="number">1</span>,</span><br><span class="line">            <span class="string">'ignore'</span> =&gt; <span class="literal">true</span>,</span><br><span class="line">            <span class="string">'duality'</span> =&gt; <span class="literal">false</span>,</span><br><span class="line">        ],</span><br><span class="line">    ],</span><br><span class="line"></span><br><span class="line">    <span class="string">'stopwords'</span> =&gt; [</span><br><span class="line">        <span class="string">'的'</span>,</span><br><span class="line">        <span class="string">'了'</span>,</span><br><span class="line">        <span class="string">'而是'</span>,</span><br><span class="line">    ],</span><br><span class="line">],</span><br><span class="line"> env 增加配置项</span><br><span class="line"></span><br><span class="line">SCOUT_DRIVER=tntsearch</span><br><span class="line">TNTSEARCH_TOKENIZER=jieba</span><br><span class="line">namespace App\Models;</span><br><span class="line"></span><br><span class="line">use Illuminate\Database\Eloquent\Model;</span><br><span class="line">use Laravel\Scout\Searchable;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Article</span> <span class="keyword">extends</span> <span class="title">Model</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    use Searchable;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 索引的字段</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @return array</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">toSearchableArray</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;only(<span class="string">'id'</span>, <span class="string">'title'</span>, <span class="string">'content'</span>);</span><br><span class="line"></span><br><span class="line">       <span class="comment">// return $this-&gt;toArray();</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">生成索引：</span><br><span class="line"></span><br><span class="line">php artisan scout:<span class="keyword">import</span> <span class="string">"App\Model\Article"</span></span><br><span class="line">https:<span class="comment">//learnku.com/articles/27617#replies</span></span><br></pre></td></tr></table></figure>
<h3 id="二分查找"><a href="#二分查找" class="headerlink" title="二分查找"></a>二分查找</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line">原理实现：有序集合从中间分为前后<span class="number">2</span>部分，当要查当数值等于中间值，直接返回；当要查询数值大于中间值，则说明要查询的数值在后半部分，那么继续二分后半部分；当要查询数值小于中间数值时，说明要查询的数值在前半部分，那么继续二分前半部分。（过滤掉一半数据查询）</span><br><span class="line"></span><br><span class="line">&lt;?php</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 递归实现</span></span><br><span class="line"><span class="comment">     * array $arr 有序数组</span></span><br><span class="line"><span class="comment">     * int $search 要查询的数字</span></span><br><span class="line"><span class="comment">     * int firstIndex 数组起始位置</span></span><br><span class="line"><span class="comment">     * int lastIndex 数组结束位置</span></span><br><span class="line"><span class="comment">     * @return </span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">binarySearchRecursion</span>(<span class="params">array $arr, $search, $lastIndex, $firstIndex = <span class="number">0</span></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $len = count($arr);</span><br><span class="line">        <span class="keyword">if</span> ($len &lt;= <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        $middle = intval(($firstIndex + $lastIndex) / <span class="number">2</span>);</span><br><span class="line">        <span class="keyword">if</span> ($search == $arr[$middle]) &#123;<span class="comment">//找到直接返回</span></span><br><span class="line">            <span class="keyword">return</span> $arr[$middle];</span><br><span class="line">        &#125; elseif ($search &gt; $arr[$middle]) &#123;<span class="comment">//去后面查，数组起始位置变为$middle + 1。</span></span><br><span class="line">            <span class="keyword">return</span> binarySearchRecursion($arr, $search, $middle + <span class="number">1</span>, $lastIndex);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;<span class="comment">//去前面查，数组结束位置为$middle - 1。</span></span><br><span class="line">            <span class="keyword">return</span> binarySearchRecursion($arr, $search, $firstIndex, $middle - <span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 递归实现</span></span><br><span class="line"><span class="comment">     * array $arr 有序数组</span></span><br><span class="line"><span class="comment">     * int $search 要查询的数字</span></span><br><span class="line"><span class="comment">     * @return </span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">     <span class="function"><span class="keyword">function</span> <span class="title">binarySearch</span>(<span class="params">$arr, $search</span>)</span></span><br><span class="line"><span class="function">     </span>&#123;</span><br><span class="line">        $len = count($arr);</span><br><span class="line">        <span class="keyword">if</span> ($len &lt;= <span class="number">0</span> ) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        $firstIndex = <span class="number">0</span>;</span><br><span class="line">        $lastIndex = $len - <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">while</span>($firstIndex &lt;= $lastIndex) &#123;</span><br><span class="line">            $middle = intval(($firstIndex + $lastIndex) / <span class="number">2</span>);</span><br><span class="line">            <span class="keyword">if</span> ($search == $arr[$middle]) &#123;<span class="comment">//出口</span></span><br><span class="line">                <span class="keyword">return</span> $arr[$middle];</span><br><span class="line">            &#125; elseif ($search &gt; $arr[$middle]) &#123;<span class="comment">//去后面查，数组起始位置变为$middle + 1。</span></span><br><span class="line">                $firstIndex = $middle + <span class="number">1</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;<span class="comment">//去前面查，数组结束位置为$middle - 1。</span></span><br><span class="line">                $lastIndex = $middle - <span class="number">1</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">     &#125;</span><br><span class="line">     https:<span class="comment">//learnku.com/articles/35240</span></span><br></pre></td></tr></table></figure>
<h3 id="子孙树"><a href="#子孙树" class="headerlink" title="子孙树"></a>子孙树</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">$arr = [</span><br><span class="line">    [<span class="string">'id'</span>=&gt;<span class="number">1</span>,<span class="string">'parent_id'</span>=&gt;<span class="number">0</span>,<span class="string">'type'</span>=&gt;<span class="number">5</span>],</span><br><span class="line">    [<span class="string">'id'</span>=&gt;<span class="number">2</span>,<span class="string">'parent_id'</span>=&gt;<span class="number">1</span>,<span class="string">'type'</span>=&gt;<span class="number">3</span>],</span><br><span class="line">    [<span class="string">'id'</span>=&gt;<span class="number">3</span>,<span class="string">'parent_id'</span>=&gt;<span class="number">0</span>,<span class="string">'type'</span>=&gt;<span class="number">1</span>],</span><br><span class="line">    [<span class="string">'id'</span>=&gt;<span class="number">4</span>,<span class="string">'parent_id'</span>=&gt;<span class="number">3</span>,<span class="string">'type'</span>=&gt;<span class="number">3</span>],</span><br><span class="line">];</span><br><span class="line">var_dump(getSubTree($arr));</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getSubTree</span>(<span class="params">$data, $parent = <span class="string">'parent_id'</span>, $son = <span class="string">'id'</span>, $pid = <span class="number">0</span></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $tmp = [];</span><br><span class="line">        foreach ($data <span class="keyword">as</span> $key =&gt; $value) &#123;</span><br><span class="line">            <span class="keyword">if</span> ($value[$parent] == $pid) &#123;</span><br><span class="line">                $value[<span class="string">'child'</span>] = getSubTree($data, $parent, $son, $value[$son]);</span><br><span class="line">                $tmp[] = $value;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> $tmp;</span><br><span class="line">    &#125;</span><br><span class="line">    http:<span class="comment">//www.putyy.com/article/42</span></span><br></pre></td></tr></table></figure>
<h3 id="Laravel-的设计哲学"><a href="#Laravel-的设计哲学" class="headerlink" title="Laravel 的设计哲学"></a>Laravel 的设计哲学</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserController</span> <span class="keyword">extends</span> <span class="title">Controller</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">index</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $users = User::all();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> view(<span class="string">'users.index'</span>, compact(<span class="string">'users'</span>));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">改为 &lt;?php</span><br><span class="line">   </span><br><span class="line">   namespace App\Repositories;</span><br><span class="line">   </span><br><span class="line">   use App\User;</span><br><span class="line">   </span><br><span class="line">   <span class="class"><span class="keyword">class</span> <span class="title">DbUserRepository</span></span></span><br><span class="line"><span class="class">   </span>&#123;</span><br><span class="line">       public <span class="function"><span class="keyword">function</span> <span class="title">all</span>(<span class="params"></span>): <span class="title">array</span></span></span><br><span class="line"><span class="function">       </span>&#123;</span><br><span class="line">           <span class="keyword">return</span> User::all()-&gt;toArray();</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br><span class="line">   </span><br><span class="line">   <span class="class"><span class="keyword">class</span> <span class="title">UserController</span> <span class="keyword">extends</span> <span class="title">Controller</span></span></span><br><span class="line"><span class="class">   </span>&#123;</span><br><span class="line">       private $users;</span><br><span class="line">   </span><br><span class="line">       public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params"> </span>)</span></span><br><span class="line"><span class="function">       </span>&#123;</span><br><span class="line">           $<span class="keyword">this</span>-&gt;users = <span class="keyword">new</span> DbUserRepository;</span><br><span class="line">           <span class="comment">// $this-&gt;users = new RedisRepository;</span></span><br><span class="line">       &#125;</span><br><span class="line">   </span><br><span class="line">       public <span class="function"><span class="keyword">function</span> <span class="title">index</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">       </span>&#123;   </span><br><span class="line">           $users = $<span class="keyword">this</span>-&gt;users-&gt;all();</span><br><span class="line">           <span class="keyword">return</span> $users;</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br><span class="line">   依赖正转的不合理之处在哪里呢？位于高层的控制器依赖于具体的底层数据获取服务，当底层发生变动时，就需要对应的修改高层的内部结构。</span><br><span class="line">   </span><br><span class="line">   我们对依赖关系进一步分析，可知控制器关注的并不是具体如何获取数据，控制器关注的是「数据的可获取性」这一抽象。因此，我们应当将依赖关系进行反转，将对依赖的具体声明职责转移到外部，让控制器仅依赖于抽象层（数据的可获取性）。这种解决方式称之为 控制反转 或 依赖倒置。通过控制反转，高层不再依赖于具体的底层，仅仅是依赖于抽象层，高层和底层实现了解耦。</span><br><span class="line">   用接口来表示「数据的可获取性」这一抽象</span><br><span class="line">   </span><br><span class="line">   &lt;?php</span><br><span class="line">   </span><br><span class="line">   namespace App\Repositories;</span><br><span class="line">   </span><br><span class="line">   interface UserRepositoryInterface</span><br><span class="line">   &#123;</span><br><span class="line">       public <span class="function"><span class="keyword">function</span> <span class="title">all</span>(<span class="params"></span>): <span class="title">array</span>;</span></span><br><span class="line"><span class="function">   &#125;</span></span><br><span class="line"><span class="function">   <span class="title">UserController</span> 依赖的是「数据的可获取性」，不依赖于具体的实现</span></span><br><span class="line"><span class="function">   </span></span><br><span class="line"><span class="function">   <span class="title">class</span> <span class="title">UserController</span> <span class="title">extends</span> <span class="title">Controller</span></span></span><br><span class="line"><span class="function">   </span>&#123;</span><br><span class="line">       private $users;</span><br><span class="line">   </span><br><span class="line">       public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">UserRepositoryInterface $users</span>)</span></span><br><span class="line"><span class="function">       </span>&#123;</span><br><span class="line">           $<span class="keyword">this</span>-&gt;users = $users;</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br><span class="line">   具体的实现交给对应的仓库类即可</span><br><span class="line">   </span><br><span class="line">   <span class="class"><span class="keyword">class</span> <span class="title">DbUserRepository</span> <span class="title">implements</span> <span class="title">UserRepositoryInterface</span> </span>&#123;&#125;</span><br><span class="line">   <span class="class"><span class="keyword">class</span> <span class="title">RedisRepository</span> <span class="title">implements</span> <span class="title">UserRepositoryInterface</span> </span>&#123; &#125;</span><br><span class="line">   根据自己的需要注入对应的服务，这样就实现了依赖注入。</span><br><span class="line">   </span><br><span class="line">   $userRepository = <span class="keyword">new</span> DbUserRepository;</span><br><span class="line">   $userController = <span class="keyword">new</span> UserController($userRepository)</span><br><span class="line">   总的来说，依赖注入由四部分构成</span><br><span class="line">   </span><br><span class="line">   被使用的服务 - DbUserRepository 或者 RedisRepository 等</span><br><span class="line">   依赖某种服务的客户端 - UserController</span><br><span class="line">   声明客户端如何依赖服务的接口 - UserRepositoryInterface</span><br><span class="line">   依赖注入器，用于决定注入哪项服务给客户端</span><br><span class="line">   </span><br><span class="line">   一个简单的服务容器的实现</span><br><span class="line">   </span><br><span class="line">   namespace App\Services;</span><br><span class="line">   </span><br><span class="line">   use Exception;</span><br><span class="line">   </span><br><span class="line">   <span class="class"><span class="keyword">class</span> <span class="title">Container</span> </span></span><br><span class="line"><span class="class">   </span>&#123;</span><br><span class="line">       protected <span class="keyword">static</span> $container = [];</span><br><span class="line">   </span><br><span class="line">       <span class="comment">/**</span></span><br><span class="line"><span class="comment">        * 绑定服务</span></span><br><span class="line"><span class="comment">        * </span></span><br><span class="line"><span class="comment">        * @param  服务名称 $name </span></span><br><span class="line"><span class="comment">        * @param  Callable $resolver</span></span><br><span class="line"><span class="comment">        * @return void</span></span><br><span class="line"><span class="comment">        */</span></span><br><span class="line">       public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">bind</span>(<span class="params">$name, Callable $resolver</span>)</span></span><br><span class="line"><span class="function">       </span>&#123;   </span><br><span class="line">           <span class="keyword">static</span>::$container[$name] = $resolver;</span><br><span class="line">       &#125;</span><br><span class="line">   </span><br><span class="line">       <span class="comment">/**</span></span><br><span class="line"><span class="comment">        * 解析服务</span></span><br><span class="line"><span class="comment">        * </span></span><br><span class="line"><span class="comment">        * @param  服务名称 $name</span></span><br><span class="line"><span class="comment">        * @return mix</span></span><br><span class="line"><span class="comment">        */</span></span><br><span class="line">       public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">make</span>(<span class="params">$name</span>)</span></span><br><span class="line"><span class="function">       </span>&#123;</span><br><span class="line">           <span class="keyword">if</span>(isset(<span class="keyword">static</span>::$container[$name]))&#123;</span><br><span class="line">               $resolver = <span class="keyword">static</span>::$container[$name];</span><br><span class="line">               <span class="keyword">return</span> $resolver();</span><br><span class="line">           &#125;</span><br><span class="line">   </span><br><span class="line">           <span class="keyword">throw</span> <span class="keyword">new</span> Exception(<span class="string">"不存在该绑定"</span>);</span><br><span class="line">      &#125;</span><br><span class="line">   </span><br><span class="line">   &#125;</span><br><span class="line">   绑定服务</span><br><span class="line">   </span><br><span class="line">   App\Services\Container::bind(<span class="string">'UserRepository'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">       <span class="keyword">return</span> <span class="keyword">new</span> App\Repositories\DbUserRepository;</span><br><span class="line">   &#125;);</span><br><span class="line">   解析服务</span><br><span class="line">   </span><br><span class="line">   $userRepository = App\Services\Container::make(<span class="string">'UserRepository'</span>);</span><br><span class="line">   $userController = <span class="keyword">new</span> UserController($userRepository)</span><br><span class="line">   Laravel 的服务容器的功能则更加的强大，比如，可以将接口与具体的实现进行绑定，通常在 服务提供者 中使用服务容器来进行绑定</span><br><span class="line">   </span><br><span class="line">   public <span class="function"><span class="keyword">function</span> <span class="title">register</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">   </span>&#123;</span><br><span class="line">       $<span class="keyword">this</span>-&gt;app-&gt;singleton(UserRepositoryInterface::<span class="class"><span class="keyword">class</span>, <span class="title">function</span> ($<span class="title">app</span>) </span>&#123;</span><br><span class="line">           <span class="keyword">return</span> <span class="keyword">new</span> UserRepository;</span><br><span class="line">       &#125;);</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>
<h3 id="高并发业务场景下的秒杀解决方案"><a href="#高并发业务场景下的秒杀解决方案" class="headerlink" title="高并发业务场景下的秒杀解决方案"></a>高并发业务场景下的秒杀解决方案</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">在秒杀前将商品的库存信息加入到 Redis 缓存中。如下格式:</span><br><span class="line">$redis-&gt;lpush(<span class="string">'商品id'</span>,<span class="number">1</span>);</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * 1.接受用户请求</span></span><br><span class="line"><span class="comment"> * 2.验证用户是否已经参与秒杀,商品是否存在</span></span><br><span class="line"><span class="comment"> * 3.根据商品id减少商品队列中的库存数量</span></span><br><span class="line"><span class="comment"> * 4.将用户的秒杀数据写入server层中,并返回秒杀数据对应的唯一key值</span></span><br><span class="line"><span class="comment"> * 5.用户点击下单,根据serve层中的缓存数据,生成订单数据并减少数据库商品的库存数据</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">$getParams = $_POST;</span><br><span class="line">$userId = $getParams[<span class="string">'userId'</span>];</span><br><span class="line">$goodsId = $getParams[<span class="string">'goodsId'</span>];</span><br><span class="line"></span><br><span class="line">$key = <span class="string">'goods:miaosha:'</span>;</span><br><span class="line">$userResult = $redis-&gt;get($key.$userId);</span><br><span class="line"><span class="keyword">if</span>($userResult)&#123;</span><br><span class="line">    $userResult = json_decode($userResult,<span class="literal">true</span>);</span><br><span class="line">    echo json_encode([<span class="string">'result'</span>=&gt;$userResult[<span class="string">'result'</span>],<span class="string">'key'</span>=&gt;$key.$userId]);<span class="comment">// 已经参与过秒杀了</span></span><br><span class="line">    die();</span><br><span class="line">&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">    $goodqueue = <span class="string">'goods:queue:'</span>.$goodsId;</span><br><span class="line">    $result = $redis-&gt;lpop($goodqueue);<span class="comment">// 删除商品redis队列缓存</span></span><br><span class="line">    <span class="keyword">if</span>($result)&#123;</span><br><span class="line">        $data = json_encode([<span class="string">'result'</span>=&gt;<span class="string">'OK'</span>,<span class="string">'userId'</span>=&gt;$userId,<span class="string">'goodsId'</span>=&gt;$goodsId]);</span><br><span class="line">        $redis-&gt;set($key.$userId,$data);<span class="comment">// 将秒杀信息写入缓存中</span></span><br><span class="line">        echo json_encode([<span class="string">'result'</span>=&gt;<span class="string">'OK'</span>,<span class="string">'userId'</span>=&gt;$userId,<span class="string">'goodsId'</span>=&gt;$goodsId,<span class="string">'key'</span>=&gt;$key.$userId]);</span><br><span class="line">        die();</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        echo json_encode([<span class="string">'result'</span>=&gt;<span class="string">'FAIL'</span>,<span class="string">'message'</span>=&gt;<span class="string">'商品不存在'</span>,<span class="string">'goodsId'</span>=&gt;$goodsId]);<span class="comment">// 商品库存不存在</span></span><br><span class="line">        die();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;<span class="comment">/**</span></span><br><span class="line"><span class="comment">  * 用户下单界面</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line"> require_once __DIR__.<span class="string">'/redis_connect.php'</span>;</span><br><span class="line"> $key = $_GET[<span class="string">'key'</span>];</span><br><span class="line"> $data = $redis-&gt;get($key);</span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment">  * 生成订单，订单入库</span></span><br><span class="line"><span class="comment">  *</span></span><br><span class="line"><span class="comment">  */</span>https:<span class="comment">//learnku.com/articles/35141</span></span><br><span class="line">redis list 可以批量插入数据，不一定每次都只插入一个值.</span><br><span class="line"></span><br><span class="line">$numberArr = range(<span class="number">1</span>,<span class="number">100</span>);  </span><br><span class="line"><span class="comment">//var_dump($numberArr);</span></span><br><span class="line">$redis-&gt;lPush(<span class="string">'goods:queue:5'</span>,...$numberArr); <span class="comment">// 可变参数</span></span><br></pre></td></tr></table></figure>
<h3 id="PHP-开启-Opcache-后如何优雅地部署-PHP-代码"><a href="#PHP-开启-Opcache-后如何优雅地部署-PHP-代码" class="headerlink" title="PHP 开启 Opcache 后如何优雅地部署 PHP 代码"></a>PHP 开启 Opcache 后如何优雅地部署 PHP 代码</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">提交了代码并且部署了以后，线上代码依然是旧的。所以我执行了下</span><br><span class="line">/etc/init.d/php-fpm reload 就生效了。</span><br><span class="line">opcache_reset (); 但是注意了 这个函数是强制清楚所有 cache 所以 如果并发较高的系统 不能直接这么搞 还有就是 它有两个清除模式，cli 下只能清除 cli 的 cache fpm 的话 必须通过 fpm 请求方式清除才行</span><br><span class="line">https:<span class="comment">//github.com/gordalina/cachetool</span></span><br><span class="line">https:<span class="comment">//learnku.com/laravel/t/35142</span></span><br></pre></td></tr></table></figure>
<h3 id="模型-query-无数据"><a href="#模型-query-无数据" class="headerlink" title="模型::query () 无数据"></a>模型::query () 无数据</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">getPost</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line"> <span class="keyword">return</span> Post::query()</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">index</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">   $post = $<span class="keyword">this</span>-&gt;getPost();</span><br><span class="line">    <span class="keyword">return</span> view(<span class="string">'index'</span>, [</span><br><span class="line">    <span class="string">'post'</span> =&gt; $post</span><br><span class="line">    ]);</span><br><span class="line">&#125;</span><br><span class="line">@foreach($post-&gt;where(<span class="string">'category_id'</span>,<span class="number">2</span>)-&gt;get() <span class="keyword">as</span> xxx)&#123;...&#125;</span><br><span class="line">@foreach($post-&gt;where(<span class="string">'category_id'</span>,<span class="number">3</span>)-&gt;get() <span class="keyword">as</span> xxx)&#123;...&#125;</span><br><span class="line">改(clone $post)-&gt;where(xxxxxxxxx)</span><br><span class="line">https:<span class="comment">//learnku.com/laravel/t/35341</span></span><br></pre></td></tr></table></figure>
<h3 id="JSON-encode-小数位丢失"><a href="#JSON-encode-小数位丢失" class="headerlink" title="JSON_encode 小数位丢失"></a>JSON_encode 小数位丢失</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">$a = <span class="string">'&#123;"orderAmt":500.00&#125;'</span>;</span><br><span class="line">$a_json_decode = json_decode($a,<span class="literal">true</span>);</span><br><span class="line">[                      </span><br><span class="line">  <span class="string">"orderAmt"</span> =&gt; <span class="number">500.0</span>, </span><br><span class="line">]                      </span><br><span class="line">$a_json_encode = json_encode($a_json_decode);</span><br><span class="line"><span class="string">"&#123;"</span>orderAmt<span class="string">":500&#125;"</span></span><br><span class="line">使用字符串类型的 <span class="number">500.00</span>，或使用字符串拼接 <span class="built_in">JSON</span> </span><br><span class="line">json_encode($a_json_decode, JSON_PRESERVE_ZERO_FRACTION)</span><br><span class="line">=&gt; <span class="string">"&#123;"</span>orderAmt<span class="string">":500.0&#125;"</span></span><br><span class="line"></span><br><span class="line">https:<span class="comment">//learnku.com/laravel/t/35311</span></span><br><span class="line"></span><br><span class="line"> 从 PHP <span class="number">5.6</span><span class="number">.6</span>+ 开始，json_encode 支持使用 JSON_PRESERVE_ZERO_FRACTION 选项以告知引擎确保浮点数始终编码为浮点数，但对于形如 <span class="number">500.00</span> （值为 <span class="number">500</span>，精确到小数点后两位的浮点数）仅能保证最终输出 <span class="number">500.0</span></span><br><span class="line"> </span><br><span class="line"> 即使临时配置 serialize_precision 为 <span class="number">10</span>，在初始化数组时使用 number_format 函数格式化小数位数，最后在 json_encode 时填入选项 JSON_PRESERVE_ZERO_FRACTION | JSON_NUMERIC_CHECK 也仅能输出 <span class="number">500.0</span>。</span><br></pre></td></tr></table></figure>
<h3 id="支付宝公钥证书-PHP-版本-SDK"><a href="#支付宝公钥证书-PHP-版本-SDK" class="headerlink" title="支付宝公钥证书 PHP 版本 SDK"></a>支付宝公钥证书 PHP 版本 SDK</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getRootCertSN</span>(<span class="params">$str</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// return '687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6';</span></span><br><span class="line">        $arr = preg_split(<span class="string">'/(?=-----BEGIN)/'</span>, $str, <span class="number">-1</span>, PREG_SPLIT_NO_EMPTY);</span><br><span class="line">        $str = <span class="literal">null</span>;</span><br><span class="line">        foreach ($arr <span class="keyword">as</span> $e) &#123;</span><br><span class="line">            $sn = getCertSN($e, <span class="literal">true</span>);</span><br><span class="line">            <span class="keyword">if</span> (!$sn) &#123;</span><br><span class="line">                <span class="keyword">continue</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> ($str === <span class="literal">null</span>) &#123;</span><br><span class="line">                $str = $sn;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                $str .= <span class="string">"_"</span> . $sn;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> $str;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">getCertSN</span>(<span class="params">$str, $matchAlgo = false</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">        根据java SDK源码：AntCertificationUtil::getRootCertSN</span></span><br><span class="line"><span class="comment">        对证书链中RSA的项目进行过滤（猜测是gm国密算法java抛错搞不定，故意略去）</span></span><br><span class="line"><span class="comment">        java源码为：</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">        if(c.getSigAlgOID().startsWith("1.2.840.113549.1.1"))</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">        根据 https://www.alvestrand.no/objectid/1.2.840.113549.1.1.html</span></span><br><span class="line"><span class="comment">        该OID为RSA算法系。</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">        <span class="keyword">if</span> ($matchAlgo) &#123;</span><br><span class="line">            openssl_x509_export($str, $out, <span class="literal">false</span>);</span><br><span class="line">            <span class="keyword">if</span> (!preg_match(<span class="string">'/Signature Algorithm:.*?RSA/im'</span>, $out, $m)) &#123;</span><br><span class="line">                <span class="keyword">return</span>;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">        $a = openssl_x509_parse($str);</span><br><span class="line">        $issuer = <span class="literal">null</span>;</span><br><span class="line">        <span class="comment">// 注意：根据java代码输出，需要倒着排列 CN,OU,O</span></span><br><span class="line">        foreach ($a[<span class="string">"issuer"</span>] <span class="keyword">as</span> $k =&gt; $v) &#123;</span><br><span class="line">            <span class="keyword">if</span> ($issuer === <span class="literal">null</span>) &#123;</span><br><span class="line">                $issuer = <span class="string">"$k=$v"</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                $issuer = <span class="string">"$k=$v,"</span> . $issuer;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        #    echo($issuer . $a["serialNumber"] . "\n");</span><br><span class="line">        $serialNumberHex = decimalNotation($a[<span class="string">'serialNumberHex'</span>]);</span><br><span class="line">        $sn = md5($issuer . $serialNumberHex);</span><br><span class="line">        <span class="keyword">return</span> $sn;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">decimalNotation</span>(<span class="params">$hex</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $dec = <span class="number">0</span>;</span><br><span class="line">        $len = strlen($hex);</span><br><span class="line">        <span class="keyword">for</span> ($i = <span class="number">1</span>; $i &lt;= $len; $i++) &#123;</span><br><span class="line">            $dec = bcadd($dec, bcmul(strval(hexdec($hex[$i - <span class="number">1</span>])), bcpow(<span class="string">'16'</span>, strval($len - $i))));</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> $dec;</span><br><span class="line">    &#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/35315</span></span><br></pre></td></tr></table></figure>
<h3 id="字符串表达式计算"><a href="#字符串表达式计算" class="headerlink" title="字符串表达式计算"></a>字符串表达式计算</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">$a = <span class="number">10</span>;</span><br><span class="line">var_dump(<span class="built_in">eval</span>(<span class="string">'return $a &gt; 5;'</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">// 输出:</span></span><br><span class="line"><span class="comment">// bool(true)</span></span><br><span class="line">system(<span class="string">'php -r "echo 1 + 2;"'</span>);</span><br><span class="line"></span><br><span class="line">echo exec(<span class="string">'php -r "echo 1 + 2;"'</span>);</span><br><span class="line">https:<span class="comment">//shockerli.net/post/php-expression-string/</span></span><br></pre></td></tr></table></figure>
<h3 id="用户登录密码改为-md5"><a href="#用户登录密码改为-md5" class="headerlink" title="用户登录密码改为 md5"></a>用户登录密码改为 md5</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br></pre></td><td class="code"><pre><span class="line">新建一个文件 Libraries，在 Libraries 目录下新建一个 MD5.php 文件</span><br><span class="line"></span><br><span class="line">namespace App\Libraries; </span><br><span class="line"></span><br><span class="line">use Illuminate\Contracts\Hashing\Hasher; </span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MD5</span> <span class="title">implements</span> <span class="title">Hasher</span> </span></span><br><span class="line"><span class="class"></span>&#123; </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * Hash the given value. </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * @param string $value </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * @return array  $options </span></span><br><span class="line"><span class="comment">     * @return string </span></span><br><span class="line"><span class="comment">     */</span> </span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">make</span>(<span class="params">$value, array $options = []</span>) </span></span><br><span class="line"><span class="function">    </span>&#123; </span><br><span class="line">        <span class="keyword">return</span> md5($value); </span><br><span class="line">    &#125; </span><br><span class="line"></span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * Check the given plain value against a hash. </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * @param string $value </span></span><br><span class="line"><span class="comment">     * @param string $hashedValue </span></span><br><span class="line"><span class="comment">     * @param array $options </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * @return bool </span></span><br><span class="line"><span class="comment">     */</span> </span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">check</span>(<span class="params">$value, $hashedValue, array $options = []</span>) </span></span><br><span class="line"><span class="function">    </span>&#123; </span><br><span class="line">        <span class="keyword">if</span>(empty($hashedValue))&#123; </span><br><span class="line">            <span class="keyword">return</span> <span class="literal">true</span>; </span><br><span class="line">        &#125; </span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;make($value) === $hashedValue; </span><br><span class="line">    &#125; </span><br><span class="line"></span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * Check if the given hash has been hashed using the given options. </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * @param string $hashedValue </span></span><br><span class="line"><span class="comment">     * @param array $options </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * @return bool </span></span><br><span class="line"><span class="comment">     */</span> </span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">needsRehash</span>(<span class="params">$hashedValue, array $options = []</span>) </span></span><br><span class="line"><span class="function">    </span>&#123; </span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>; </span><br><span class="line">    &#125; </span><br><span class="line">&#125; </span><br><span class="line"> Providers 文件下面新建一个文件 MD5ServiceProvider.php</span><br><span class="line"> namespace App\Providers; </span><br><span class="line"> </span><br><span class="line"> use Illuminate\Auth\EloquentUserProvider; </span><br><span class="line"> <span class="class"><span class="keyword">class</span> <span class="title">MD5ServiceProvider</span> <span class="keyword">extends</span> <span class="title">EloquentUserProvider</span> </span></span><br><span class="line"><span class="class"> </span>&#123; </span><br><span class="line"> </span><br><span class="line">     <span class="comment">//继承EloquentUserProvider类，调用父类的构造函数 </span></span><br><span class="line">     public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">$hasher, $model</span>) </span></span><br><span class="line"><span class="function">     </span>&#123; </span><br><span class="line">         parent::__construct($hasher, $model); </span><br><span class="line">     &#125; </span><br><span class="line"> </span><br><span class="line">     <span class="comment">/** </span></span><br><span class="line"><span class="comment">      * Bootstrap the application services. </span></span><br><span class="line"><span class="comment">      * </span></span><br><span class="line"><span class="comment">      * @return void </span></span><br><span class="line"><span class="comment">      */</span> </span><br><span class="line">     public <span class="function"><span class="keyword">function</span> <span class="title">boot</span>(<span class="params"></span>) </span></span><br><span class="line"><span class="function">     </span>&#123; </span><br><span class="line">         <span class="comment">// </span></span><br><span class="line">     &#125; </span><br><span class="line"> </span><br><span class="line">     <span class="comment">/** </span></span><br><span class="line"><span class="comment">      * Register the application services. </span></span><br><span class="line"><span class="comment">      * </span></span><br><span class="line"><span class="comment">      * @return void </span></span><br><span class="line"><span class="comment">      */</span> </span><br><span class="line">     public <span class="function"><span class="keyword">function</span> <span class="title">register</span>(<span class="params"></span>) </span></span><br><span class="line"><span class="function">     </span>&#123; </span><br><span class="line">         <span class="comment">// </span></span><br><span class="line">     &#125; </span><br><span class="line"> &#125; </span><br><span class="line">  AuthServiceProvider.php 文件里 boot 方法里添加如下代码</span><br><span class="line"> </span><br><span class="line">         Auth::provider(<span class="string">'MD5'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">$app</span>) </span>&#123; </span><br><span class="line">             $model = config(<span class="string">'auth.providers.users.model'</span>); </span><br><span class="line">             <span class="keyword">return</span> <span class="keyword">new</span> MD5ServiceProvider(<span class="keyword">new</span> MD5, $model); </span><br><span class="line">         &#125;); </span><br><span class="line">修改 config/auth.php 里的 providers</span><br><span class="line"><span class="string">'providers'</span> =&gt; [</span><br><span class="line">        <span class="string">'users'</span> =&gt; [</span><br><span class="line">            <span class="string">'driver'</span> =&gt; <span class="string">'MD5'</span>,<span class="comment">//'driver' =&gt; 'eloquent',//eloquent默认加密码方式</span></span><br><span class="line">            <span class="string">'model'</span> =&gt; App\User::<span class="class"><span class="keyword">class</span>,</span></span><br><span class="line">        ],</span><br><span class="line">        <span class="string">'admins'</span> =&gt; [</span><br><span class="line">            <span class="string">'driver'</span> =&gt; <span class="string">'eloquent'</span>,</span><br><span class="line">            <span class="string">'model'</span> =&gt; App\Admin::<span class="class"><span class="keyword">class</span>,</span></span><br><span class="line">        ],</span><br><span class="line">        修改 app/Http/Controllers/Auth/RegisterController.php 里的 create，修改代码如下</span><br><span class="line">        </span><br><span class="line">            protected <span class="function"><span class="keyword">function</span> <span class="title">create</span>(<span class="params">array $data</span>)</span></span><br><span class="line"><span class="function">            </span>&#123;</span><br><span class="line">                <span class="keyword">return</span> User::create([</span><br><span class="line">                    <span class="string">'name'</span> =&gt; $data[<span class="string">'name'</span>],</span><br><span class="line">                    <span class="string">'email'</span> =&gt; $data[<span class="string">'email'</span>],</span><br><span class="line">                    <span class="string">'password'</span> =&gt; md5($data[<span class="string">'password'</span>]),</span><br><span class="line">                ]);</span><br><span class="line">            &#125;</span><br><span class="line">            https:<span class="comment">//learnku.com/articles/35407</span></span><br></pre></td></tr></table></figure>
<h3 id="Laravel跨库跨连接的事务操作"><a href="#Laravel跨库跨连接的事务操作" class="headerlink" title="Laravel跨库跨连接的事务操作"></a>Laravel跨库跨连接的事务操作</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">Laravel 下的跨库事务操作是基于连接的 当执行 DB::beginTransaction(); 的时候 其实是和默认的数据库配置建立了连接 后面的操作 commit 或者 rollback 都是操作的这个默认数据库 如果在这中间操作了其他的数据库 对他是不生效的</span><br><span class="line">同时 commit 和 rollback 都 指定连接</span><br><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">    <span class="comment">//开启默认数据库的事务</span></span><br><span class="line">    DB::beginTransaction();</span><br><span class="line">    <span class="comment">//开启test数据库的事务</span></span><br><span class="line">    DB::connection(<span class="string">'test'</span>)-&gt;beginTransaction();</span><br><span class="line">    <span class="comment">//中间各种数据库操作</span></span><br><span class="line">    Table1::xxxxxx();</span><br><span class="line">    Table2::xxxxxx();</span><br><span class="line">    <span class="keyword">if</span> (<span class="literal">true</span>) &#123;</span><br><span class="line">        <span class="comment">//一起提交</span></span><br><span class="line">        DB::commit();</span><br><span class="line">        DB::connection(<span class="string">'test'</span>)-&gt;commit();</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">//一起回滚</span></span><br><span class="line">        DB::rollback();</span><br><span class="line">        DB::connection(<span class="string">'test'</span>)-&gt;rollback();</span><br><span class="line">    &#125;</span><br><span class="line">&#125; <span class="keyword">catch</span> (\Exception $exception) &#123;</span><br><span class="line">    echo <span class="string">"catch some errors:"</span>.$exception-&gt;getMessage();</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//caihongtengxu.github.io/2018/20181009/index.html</span></span><br></pre></td></tr></table></figure>
<h3 id="array-splice无法自定义键值"><a href="#array-splice无法自定义键值" class="headerlink" title="array_splice无法自定义键值"></a>array_splice无法自定义键值</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">array_insert</span> (<span class="params">&amp;$array, $position, $insert_array</span>) </span>&#123;</span><br><span class="line">            $first_array = array_splice ($array, <span class="number">0</span>, $position);</span><br><span class="line">            $array = array_merge ($first_array, $insert_array, $array);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        $arr = array(</span><br><span class="line">            <span class="string">'tt'</span> =&gt; <span class="number">1333</span>,</span><br><span class="line">            <span class="string">'cc'</span> =&gt; <span class="number">333</span>,</span><br><span class="line">            <span class="string">'aaz'</span> =&gt; <span class="number">2333</span>,</span><br><span class="line">            <span class="string">'ee'</span> =&gt; <span class="number">78</span>,</span><br><span class="line">        );</span><br><span class="line">        $temp[<span class="string">"bb"</span>] = <span class="number">33</span>;</span><br><span class="line">        array_insert($arr,<span class="number">1</span>,$temp);</span><br><span class="line">&gt;&gt;&gt; $arr</span><br><span class="line">=&gt; [</span><br><span class="line">     <span class="string">"tt"</span> =&gt; <span class="number">1333</span>,</span><br><span class="line">     <span class="string">"bb"</span> =&gt; <span class="number">33</span>,</span><br><span class="line">     <span class="string">"cc"</span> =&gt; <span class="number">333</span>,</span><br><span class="line">     <span class="string">"aaz"</span> =&gt; <span class="number">2333</span>,</span><br><span class="line">     <span class="string">"ee"</span> =&gt; <span class="number">78</span>,</span><br><span class="line">   ]</span><br></pre></td></tr></table></figure>
<h3 id="数字转度量"><a href="#数字转度量" class="headerlink" title="数字转度量"></a>数字转度量</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 数字转度量</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * @param int $num 数字</span></span><br><span class="line"><span class="comment"> * @return string|int</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">num2metric</span>(<span class="params">$num, $precision = <span class="number">0</span></span>) </span>&#123;</span><br><span class="line">    $unitList = [</span><br><span class="line">        <span class="string">'P'</span> =&gt; <span class="number">15</span>,</span><br><span class="line">        <span class="string">'T'</span> =&gt; <span class="number">12</span>,</span><br><span class="line">        <span class="string">'G'</span> =&gt; <span class="number">9</span>,</span><br><span class="line">        <span class="string">'M'</span> =&gt; <span class="number">6</span>,</span><br><span class="line">        <span class="string">'W'</span> =&gt; <span class="number">4</span>,</span><br><span class="line">        <span class="string">'K'</span> =&gt; <span class="number">3</span>,</span><br><span class="line">    ];</span><br><span class="line"></span><br><span class="line">    $num = (int) $num;</span><br><span class="line"></span><br><span class="line">    foreach($unitList <span class="keyword">as</span> $name =&gt; $pow) &#123;</span><br><span class="line">        $size = pow(<span class="number">10</span>, $pow);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span>($num &gt;= $size) &#123;</span><br><span class="line">            <span class="keyword">return</span> round($num / $size * <span class="number">100</span>, $precision) / <span class="number">100</span> . $name;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> $num;</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//www.hongfs.cn/2018/10/php/php-metric-prefix/</span></span><br><span class="line">num2metric(<span class="number">1000</span>) <span class="comment">// 1K</span></span><br><span class="line">num2metric(<span class="string">'10000'</span>) <span class="comment">// 1W</span></span><br><span class="line">num2metric(<span class="string">'A'</span>) <span class="comment">// 0</span></span><br></pre></td></tr></table></figure>
<h3 id="获取两个坐标之间距离"><a href="#获取两个坐标之间距离" class="headerlink" title="获取两个坐标之间距离"></a>获取两个坐标之间距离</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 获取两个坐标之间距离</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * @param int|float $lat1 第一个坐标纬度</span></span><br><span class="line"><span class="comment"> * @param int|float $lon1 第一个坐标经度</span></span><br><span class="line"><span class="comment"> * @param int|float $lat2 第一个坐标纬度</span></span><br><span class="line"><span class="comment"> * @param int|float $lon2 第二个坐标经度</span></span><br><span class="line"><span class="comment"> * @param string $unit 距离单位 M 法定英里 K 公里 N 海里</span></span><br><span class="line"><span class="comment"> * @return int|float</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getDistanceBetweenPoints</span>(<span class="params">$lat1, $lon1, $lat2, $lon2, $unit = <span class="string">'K'</span></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (($lat1 == $lat2) &amp;&amp; ($lon1 == $lon2)) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    $theta = $lon1 - $lon2;</span><br><span class="line">    $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));</span><br><span class="line">    $dist = acos($dist);</span><br><span class="line">    $dist = rad2deg($dist);</span><br><span class="line">    $miles = $dist * <span class="number">60</span> * <span class="number">1.1515</span>;</span><br><span class="line">    $unit = strtoupper($unit);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> ($unit === <span class="string">'K'</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> $miles * <span class="number">1.609344</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> ($unit === <span class="string">'N'</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> $miles * <span class="number">0.8684</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> $miles;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;https:<span class="comment">//www.hongfs.cn/2019/06/php/php-get-distance-between-points/</span></span><br><span class="line">getDistanceBetweenPoints(<span class="number">113.276885</span>, <span class="number">23.090654</span>, <span class="number">113.320331</span>, <span class="number">23.096197</span>);</span><br><span class="line"><span class="comment">// PHP: 4.8368890520256 公里</span></span><br><span class="line"><span class="comment">// 高德地图: 4491 米</span></span><br><span class="line"><span class="comment">//restapi.amap.com/v3/distance？key-您的key&amp;origins=113.276885，23.090654&amp;destination=113.320331，23.096197&amp;type=e</span></span><br></pre></td></tr></table></figure>
<h3 id="Laravel-上传图片"><a href="#Laravel-上传图片" class="headerlink" title="Laravel 上传图片"></a>Laravel 上传图片</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 后缀名验证</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * @param  string  $extension  后缀名</span></span><br><span class="line"><span class="comment"> * @return bool</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">protected <span class="function"><span class="keyword">function</span> <span class="title">has_extension</span>(<span class="params">string $extension</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> in_array($extension, [<span class="string">'jpg'</span>, <span class="string">'jpeg'</span>, <span class="string">'png'</span>, <span class="string">'gif'</span>, <span class="string">'bmp'</span>]);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 图片上传</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * @param  \Illuminate\Http\Request  $request</span></span><br><span class="line"><span class="comment"> * @return \Illuminate\Http\Response</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">index</span>(<span class="params">Request $request</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 获取上传键值</span></span><br><span class="line">    $name = $request-&gt;input(<span class="string">'name'</span>, <span class="string">'file'</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span>($request-&gt;hasFile($name)) &#123;</span><br><span class="line">        <span class="comment">// 文件格式上传</span></span><br><span class="line"></span><br><span class="line">        $file = $request-&gt;file($name);</span><br><span class="line">        $extension = $file-&gt;extension();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span>(!$<span class="keyword">this</span>-&gt;has_extension($extension)) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="string">'格式错误'</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        $filename = $file-&gt;store();</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span>(preg_match(<span class="string">'/^(data:\s*image\/(\w+);base64,)/'</span>, $request-&gt;input($name), $matches)) &#123;</span><br><span class="line">        <span class="comment">// base64 上传</span></span><br><span class="line"></span><br><span class="line">        $extension = $matches[<span class="number">2</span>];</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span>(!$<span class="keyword">this</span>-&gt;has_extension($extension)) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="string">'格式错误'</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        $file = $request-&gt;input($name);</span><br><span class="line">        $file = preg_replace(<span class="string">'/^(data:\s*image\/(\w+);base64,)/'</span>, <span class="string">''</span>, $file);</span><br><span class="line">        $file = str_replace(<span class="string">' '</span>, <span class="string">'+'</span>, $file);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 生成保存文件名</span></span><br><span class="line">        $filename = str_random(<span class="number">40</span>) . <span class="string">'.'</span> . $extension;</span><br><span class="line">        Storage::put($filename, base64_decode($file));</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">'格式错误'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="string">'上传成功['</span> . $filename . <span class="string">']'</span>;</span><br><span class="line">&#125;https:<span class="comment">//www.hongfs.cn/2019/06/php/laravel/laravel-upload-image/</span></span><br></pre></td></tr></table></figure>
<h3 id="拼音排序"><a href="#拼音排序" class="headerlink" title="拼音排序"></a>拼音排序</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line">$ composer <span class="built_in">require</span> <span class="string">"overtrue/pinyin:~4.0"</span></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 地区列表 - 拼音排序</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @return \Illuminate\Http\Response</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">list</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $list_tmp = DB::table(<span class="string">'area'</span>)</span><br><span class="line">                        -&gt;select(<span class="string">'code'</span>, <span class="string">'name'</span>)</span><br><span class="line">                        -&gt;orderBy(DB::raw(<span class="string">'convert(name using gbk)'</span>))</span><br><span class="line">                        -&gt;get();</span><br><span class="line"></span><br><span class="line">        $pinyin = <span class="keyword">new</span> Pinyin();</span><br><span class="line"></span><br><span class="line">        $list = [];</span><br><span class="line"></span><br><span class="line">        $list_tmp-&gt;map(<span class="function"><span class="keyword">function</span>(<span class="params">$item</span>) <span class="title">use</span>(<span class="params">&amp;$list, $pinyin</span>) </span>&#123;</span><br><span class="line">            <span class="comment">// 去除城市名最后面的市字</span></span><br><span class="line">            <span class="comment">// if(substr($item-&gt;name, -3) === '市') &#123;</span></span><br><span class="line">            <span class="comment">//     $item-&gt;name = substr($item-&gt;name, 0, -3);</span></span><br><span class="line">            <span class="comment">// &#125;</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">// 获取城市名第一个字</span></span><br><span class="line">            $name_first = substr($item-&gt;name, <span class="number">0</span>, <span class="number">3</span>);</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 获取拼音</span></span><br><span class="line">            $name_pinyin = $pinyin-&gt;convert($name_first)[<span class="number">0</span>];</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 获取第一个字母并且转换为大写</span></span><br><span class="line">            $name_pinyin = strtoupper(substr($name_pinyin, <span class="number">0</span>, <span class="number">1</span>));</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span>(!isset($list[$name_pinyin])) &#123;</span><br><span class="line">                $list[$name_pinyin] = [];</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            $list[$name_pinyin][] = (array) $item;</span><br><span class="line">        &#125;);</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="string">"code"</span>: <span class="number">1</span>,</span><br><span class="line">            <span class="string">"data"</span>: &#123;</span><br><span class="line">                <span class="string">"G"</span>: [</span><br><span class="line">                    &#123;</span><br><span class="line">                        <span class="string">"code"</span>: <span class="number">5108</span>,</span><br><span class="line">                        <span class="string">"name"</span>: <span class="string">"广元市"</span></span><br><span class="line">                    &#125;,</span><br><span class="line">                    &#123;</span><br><span class="line">                        <span class="string">"code"</span>: <span class="number">4401</span>,</span><br><span class="line">                        <span class="string">"name"</span>: <span class="string">"广州市"</span></span><br><span class="line">                    &#125;</span><br><span class="line">                ],</span><br><span class="line">                <span class="string">"S"</span>: [</span><br><span class="line">                    &#123;</span><br><span class="line">                        <span class="string">"code"</span>: <span class="number">4403</span>,</span><br><span class="line">                        <span class="string">"name"</span>: <span class="string">"深圳市"</span></span><br><span class="line">                    &#125;</span><br><span class="line">                ]</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">https:<span class="comment">//www.hongfs.cn/2019/09/php/laravel/laravel-area-list-pinyin-sort/</span></span><br></pre></td></tr></table></figure>
<h3 id="PHP读取微信客户端数据库"><a href="#PHP读取微信客户端数据库" class="headerlink" title="PHP读取微信客户端数据库"></a>PHP读取微信客户端数据库</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">  <span class="class"><span class="keyword">class</span> <span class="title">MyDB</span> <span class="keyword">extends</span> <span class="title">SQLite3</span></span></span><br><span class="line"><span class="class">   </span>&#123;</span><br><span class="line">      <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">      </span>&#123;</span><br><span class="line">      <span class="comment">//根据sqlite提供的open接口，输入密钥key，</span></span><br><span class="line">      <span class="comment">//$this-&gt;open('EnMicroMsg.db',SQLITE3_OPEN_READWRITE,'71ca1d4');</span></span><br><span class="line">         $<span class="keyword">this</span>-&gt;open(<span class="string">'test.db'</span>);<span class="comment">//test.db存在就链接，不存在就创建</span></span><br><span class="line">      &#125;</span><br><span class="line">   &#125;</span><br><span class="line">   $db = <span class="keyword">new</span> MyDB();</span><br><span class="line">   <span class="keyword">if</span>(!$db)&#123;</span><br><span class="line">      echo $db-&gt;lastErrorMsg();</span><br><span class="line">   &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      echo <span class="string">"Opened database successfully\n"</span>;</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   $sql =&lt;&lt;&lt;EOF</span><br><span class="line">      SELECT * from COMPANY;</span><br><span class="line">EOF;</span><br><span class="line"></span><br><span class="line">   $ret = $db-&gt;query($sql);</span><br><span class="line">   while($row = $ret-&gt;fetchArray(SQLITE3_ASSOC) )&#123;</span><br><span class="line">      echo "ID = ". $row['ID'] . "\n";</span><br><span class="line">      echo "NAME = ". $row['NAME'] ."\n";</span><br><span class="line">      echo "ADDRESS = ". $row['ADDRESS'] ."\n";</span><br><span class="line">      echo "SALARY =  ".$row['SALARY'] ."\n\n";</span><br><span class="line">   &#125;</span><br><span class="line">   echo "Operation done successfully\n";</span><br><span class="line">   $db-&gt;close();</span><br><span class="line">https://learnku.com/laravel/t/35478</span><br></pre></td></tr></table></figure>
<h3 id="PHP7-4-可能会对-Laravel-生态带来的改变"><a href="#PHP7-4-可能会对-Laravel-生态带来的改变" class="headerlink" title="PHP7.4 可能会对 Laravel 生态带来的改变"></a>PHP7.4 可能会对 Laravel 生态带来的改变</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">php <span class="number">7.4</span> alpha 版本已经发布了，新特性很多，不过我只关注了预加载（preload）特性。</span><br><span class="line">我们都知道，laravel include 的文件比较多，会带来较大的磁盘 io, 预加载则直接解决了这个问题。https:<span class="comment">//learnku.com/laravel/t/30557  </span></span><br><span class="line">fpm 基本不会成为你的并发瓶颈，数据库往往率先成为并发的瓶颈。</span><br><span class="line">只有在网络通讯应用中，fpm 才有可能成为瓶颈，但是，fpm 也不能作为通讯组件来使用啊</span><br><span class="line"></span><br><span class="line">开启 preload 后，对于需要加载很多文件的项目来说，性能直接有 <span class="number">13</span>%~<span class="number">16</span>% 的提升，那些加载文件过多的框架和项目将会得到和加载一个文件的项目接近的性能</span><br><span class="line">这个特性，是直击 laravel 的痛点的。</span><br><span class="line">我们都知道，laravel 这个框架，加载了很多文件导致性能比一般框架要慢，这个预加载功能，可以助推 laravel 的普及</span><br><span class="line"></span><br><span class="line">php 的运行模式本来就有 fpm 和 cli 两种，其中 fpm 属于 php 的一种特色，目的是简化 web 开发，从而让开发者专注于创作，而不是处理技术细节。preload 的灵感来自于 java 的 HotSpot（jvm 的黑科技，可以做到平均 gc 时长仅为 <span class="number">1</span> 毫秒），也就是在不增加开发复杂度的情况下，对性能进行 <span class="number">16</span>%～<span class="number">16</span>% 的透明提升，这对于有性能提升需求，但又不想改变 fpm 模式的项目来说意义是很大的。</span><br><span class="line"></span><br><span class="line">如果你的项目规模超大（我对超大的定义是 uv / 日大于 <span class="number">2</span> 亿），如果还继续使用 php 的话，那么就可以用 php 的 cli 模式自定义 http 服务，通过 epool 或者 sellect 实现 io 异步轮询，php 文件直接常驻内存，对于性能的提升（对比于 fpm）通常会有 <span class="number">20</span> 到 <span class="number">80</span> 倍的性能提升。跟此相关的解决方案有 workerman，swoole 等。</span><br><span class="line"></span><br><span class="line">刚才没事跑到 thinkphp 官网上，看到了他们把 workman 作为了框架的一部分，用 workman 启动一个 httpServer，就可以切换到常驻内存模式，在编码上会有一点改变，比如 fpm 模式下的 session，文件上传，和 cli 的就不是一回事了，但是如果项目本身使用了 thinkphp 框架封装好的 seesion 或者文件上传方法，那么就不会有问题，这意味着，如果前期你为了赶进度使用了 fpm 模式，那么到了后期，你仍然有无缝切换到 cli 模式的选择权。</span><br></pre></td></tr></table></figure>
<h3 id="无限极分类"><a href="#无限极分类" class="headerlink" title="无限极分类"></a>无限极分类</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br></pre></td><td class="code"><pre><span class="line">$arrs = [</span><br><span class="line">    [</span><br><span class="line">        <span class="string">'id'</span>=&gt;<span class="number">1</span>,</span><br><span class="line">        <span class="string">'parent_id'</span>=&gt;<span class="number">0</span></span><br><span class="line">    ],</span><br><span class="line">    [</span><br><span class="line">        <span class="string">'id'</span>=&gt;<span class="number">2</span>,</span><br><span class="line">        <span class="string">'parent_id'</span>=&gt;<span class="number">1</span></span><br><span class="line">    ],</span><br><span class="line">    [</span><br><span class="line">        <span class="string">'id'</span>=&gt;<span class="number">3</span>,</span><br><span class="line">        <span class="string">'parent_id'</span>=&gt;<span class="number">2</span></span><br><span class="line">    ],</span><br><span class="line">    [</span><br><span class="line">        <span class="string">'id'</span>=&gt;<span class="number">4</span>,</span><br><span class="line">        <span class="string">'parent_id'</span>=&gt;<span class="number">0</span></span><br><span class="line">    ],</span><br><span class="line">    [</span><br><span class="line">        <span class="string">'id'</span>=&gt;<span class="number">5</span>,</span><br><span class="line">        <span class="string">'parent_id'</span>=&gt;<span class="number">0</span></span><br><span class="line">    ],</span><br><span class="line">];<span class="function"><span class="keyword">function</span> <span class="title">children</span>(<span class="params">$id,$arrs</span>)</span>&#123;</span><br><span class="line">      $result =[];</span><br><span class="line">      foreach ($arrs <span class="keyword">as</span> $v)&#123;</span><br><span class="line">          <span class="keyword">if</span>($id==$v[<span class="string">'parent_id'</span>])&#123;</span><br><span class="line">              $result[]=$v;</span><br><span class="line">          &#125;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">return</span> $result;</span><br><span class="line">  &#125;</span><br><span class="line">&gt;&gt;&gt; children(<span class="number">0</span>,$arrs)</span><br><span class="line">=&gt; [</span><br><span class="line">     [</span><br><span class="line">       <span class="string">"id"</span> =&gt; <span class="number">1</span>,</span><br><span class="line">       <span class="string">"parent_id"</span> =&gt; <span class="number">0</span>,</span><br><span class="line">     ],</span><br><span class="line">     [</span><br><span class="line">       <span class="string">"id"</span> =&gt; <span class="number">4</span>,</span><br><span class="line">       <span class="string">"parent_id"</span> =&gt; <span class="number">0</span>,</span><br><span class="line">     ],</span><br><span class="line">     [</span><br><span class="line">       <span class="string">"id"</span> =&gt; <span class="number">5</span>,</span><br><span class="line">       <span class="string">"parent_id"</span> =&gt; <span class="number">0</span>,</span><br><span class="line">     ],</span><br><span class="line">   ]</span><br><span class="line">   </span><br><span class="line">   <span class="function"><span class="keyword">function</span> <span class="title">allChildren</span>(<span class="params">$id,$arrs</span>)</span>&#123;</span><br><span class="line">   </span><br><span class="line">       $result = [];</span><br><span class="line">       $children = children($id,$arrs);<span class="comment">//获取儿子数组</span></span><br><span class="line">       foreach($children <span class="keyword">as</span> $k=&gt;$child)&#123;</span><br><span class="line">           $result[$k]=$child;</span><br><span class="line">           $childResult = allChildren($child[<span class="string">'id'</span>],$arrs);<span class="comment">//获取儿子的儿子的儿子无穷尽也</span></span><br><span class="line">           foreach ($childResult <span class="keyword">as</span> $subChild) &#123;</span><br><span class="line">               $child[<span class="string">'children'</span>][]=$subChild;</span><br><span class="line">               $result[$k] = $child;</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line">       <span class="keyword">return</span> $result;</span><br><span class="line">   &#125;</span><br><span class="line">   </span><br><span class="line">   <span class="comment">//实现类似于children($id,$arrs)方法</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">children</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">           <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;hasMany(get_class($<span class="keyword">this</span>),<span class="string">'parent_id'</span>);</span><br><span class="line">       &#125;</span><br><span class="line">   <span class="comment">//实现了上面的allChildren($id,$arrs)方法</span></span><br><span class="line">   public <span class="function"><span class="keyword">function</span> <span class="title">getAllChildren</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">       </span>&#123;</span><br><span class="line">           $result = [];</span><br><span class="line">           $children = $<span class="keyword">this</span>-&gt;children;</span><br><span class="line">   </span><br><span class="line">           foreach ($children <span class="keyword">as</span> $child) &#123;</span><br><span class="line">               $result[] = $child;</span><br><span class="line">   </span><br><span class="line">               $childResult = $child-&gt;getAllChildren();</span><br><span class="line">               foreach ($childResult <span class="keyword">as</span> $subChild) &#123;</span><br><span class="line">                   $result[] = $subChild;</span><br><span class="line">               &#125;</span><br><span class="line">           &#125;</span><br><span class="line">   </span><br><span class="line">           <span class="keyword">return</span> $result;</span><br><span class="line">       &#125;</span><br><span class="line">   测试</span><br><span class="line">   https:<span class="comment">//learnku.com/articles/12466/unlimited-classification#reply52473</span></span><br><span class="line">   Model::find(<span class="number">1</span>)-&gt;getAllChildren();</span><br><span class="line">   public <span class="function"><span class="keyword">function</span> <span class="title">children</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;hasMany(get_class($<span class="keyword">this</span>), <span class="string">'parent_id'</span> );</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">allChildren</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;children()-&gt;<span class="keyword">with</span>( <span class="string">'allChildren'</span> );</span><br><span class="line">    &#125;</span><br><span class="line">    https:<span class="comment">//github.com/betterde/tree</span></span><br><span class="line">    $categorys =  children::<span class="keyword">with</span>(<span class="string">'allChildren'</span>)-&gt;first();</span><br><span class="line">    $arr = [];</span><br><span class="line">        array_walk_recursive($categories,<span class="function"><span class="keyword">function</span> (<span class="params">$v, $k</span>) <span class="title">use</span>(<span class="params">&amp;$arr</span>) </span>&#123;</span><br><span class="line">            <span class="keyword">if</span>($k == <span class="string">'id'</span>)</span><br><span class="line">                $arr[] = $v;</span><br><span class="line">        &#125;);   </span><br><span class="line">        https:<span class="comment">//segmentfault.com/a/1190000010359094</span></span><br><span class="line">        Laravel 的无限级分类插件 https:<span class="comment">//github.com/lazychaser/laravel-nestedset</span></span><br></pre></td></tr></table></figure>
<h3 id="限制请求频率中间件"><a href="#限制请求频率中间件" class="headerlink" title="限制请求频率中间件"></a>限制请求频率中间件</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">use Closure;</span><br><span class="line">use Illuminate\Support\Facades\Cache;</span><br><span class="line">use App\Libs\Xres;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ThrottleRequests</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Handle an incoming request.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @param  \Illuminate\Http\Request  $request</span></span><br><span class="line"><span class="comment">     * @param  \Closure  $next</span></span><br><span class="line"><span class="comment">     * @return mixed</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">handle</span>(<span class="params">$request, Closure $next</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $unique = $request-&gt;getClientIp() . $request-&gt;getRequestUri();</span><br><span class="line">        <span class="keyword">if</span> (Cache::has(<span class="string">'ip_list'</span>)) &#123;</span><br><span class="line">            $ip_list = Cache::get(<span class="string">'ip_list'</span>);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            $ip_list = array();</span><br><span class="line">        &#125;</span><br><span class="line">        $index = array_search($unique, array_column($ip_list, <span class="string">'ip'</span>));</span><br><span class="line">        <span class="keyword">if</span> (<span class="literal">false</span> === $index) &#123;</span><br><span class="line">            array_push($ip_list, [<span class="string">'ip'</span> =&gt; $unique, <span class="string">'time'</span> =&gt; $<span class="keyword">this</span>-&gt;time()]);</span><br><span class="line">            Cache::put(<span class="string">'ip_list'</span>, $ip_list, <span class="number">1440</span>);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            $gap = $<span class="keyword">this</span>-&gt;time() - $ip_list[$index][<span class="string">'time'</span>];</span><br><span class="line">            $ip_list[$index][<span class="string">'time'</span>] = $<span class="keyword">this</span>-&gt;time();</span><br><span class="line">            Cache::put(<span class="string">'ip_list'</span>, $ip_list, <span class="number">1</span>);</span><br><span class="line">            <span class="keyword">if</span> ($gap &lt; <span class="number">1000</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> Xres::error(<span class="string">"请求频率过快"</span>, <span class="number">200</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> $next($request);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private <span class="function"><span class="keyword">function</span> <span class="title">time</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> (int)(microtime(<span class="literal">true</span>)*<span class="number">1000</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/35823</span></span><br></pre></td></tr></table></figure>
<h3 id="Laravel-MongoDB-数据库查询"><a href="#Laravel-MongoDB-数据库查询" class="headerlink" title="Laravel MongoDB 数据库查询"></a>Laravel MongoDB 数据库查询</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">composer <span class="built_in">require</span> jenssegers/mongodb</span><br><span class="line">$capsule-&gt;getDatabaseManager()-&gt;extend(<span class="string">'mongodb'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">$config</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> Jenssegers\Mongodb\Connection($config);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h3 id="树状数据结构存储方式"><a href="#树状数据结构存储方式" class="headerlink" title="树状数据结构存储方式"></a>树状数据结构存储方式</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><span class="line">CREATE TABLE <span class="string">`categories`</span> (</span><br><span class="line">  <span class="string">`id`</span> int(<span class="number">11</span>) NOT NULL AUTO_INCREMENT,</span><br><span class="line">  <span class="string">`title`</span> char(<span class="number">100</span>) NOT NULL,</span><br><span class="line">  <span class="string">`pid`</span> int(<span class="number">11</span>) DEFAULT <span class="number">0</span>,</span><br><span class="line">  PRIMARY KEY (<span class="string">`id`</span>)</span><br><span class="line">) ENGINE=InnoDB;</span><br><span class="line"></span><br><span class="line">CREATE TABLE <span class="string">`categories`</span> (</span><br><span class="line">  <span class="string">`id`</span> int(<span class="number">11</span>) NOT NULL AUTO_INCREMENT,</span><br><span class="line">  <span class="string">`title`</span> char(<span class="number">100</span>) NOT NULL,</span><br><span class="line">  <span class="string">`lft`</span> int(<span class="number">11</span>) NOT NULL CHECK (lft&gt; <span class="number">0</span>),</span><br><span class="line">  <span class="string">`rgt`</span> int(<span class="number">11</span>) NOT NULL CHECK (rgt&gt; <span class="number">1</span>),</span><br><span class="line">  PRIMARY KEY (<span class="string">`id`</span>)</span><br><span class="line">) ENGINE=InnoDB;</span><br><span class="line"></span><br><span class="line">lft 和 rgt 是作为集合的边界，两者差值越大，则集合越大，里面的元素就越多。</span><br><span class="line">根据子集，查找父级的分类</span><br><span class="line"></span><br><span class="line">SELECT c2.* </span><br><span class="line">  FROM categories <span class="keyword">as</span> c1, categories <span class="keyword">as</span> c2</span><br><span class="line">  WHERE c1.lft BETWEEN c2.lft and c2.rgt </span><br><span class="line">      AND c1.title = <span class="string">'华为'</span>;</span><br><span class="line"></span><br><span class="line">+----+-------------+-----+-----+</span><br><span class="line">| id | title       | lft | rgt |</span><br><span class="line">+----+-------------+-----+-----+</span><br><span class="line">|  <span class="number">1</span> | Smartphones |   <span class="number">1</span> |  <span class="number">14</span> |</span><br><span class="line">|  <span class="number">5</span> | Harmony OS  |  <span class="number">10</span> |  <span class="number">13</span> |</span><br><span class="line">|  <span class="number">8</span> | 华为        |  <span class="number">11</span> |  <span class="number">12</span> |</span><br><span class="line">+----+-------------+-----+-----+</span><br><span class="line">根据父级，查找其底下所有的子集</span><br><span class="line"></span><br><span class="line">SELECT c1.*</span><br><span class="line">   FROM categories AS c1, categories AS c2</span><br><span class="line">  WHERE c1.lft BETWEEN c2.lft AND c2.rgt</span><br><span class="line">    AND c2.title = <span class="string">'Smartphones'</span>;</span><br><span class="line"></span><br><span class="line">+----+-------------+-----+-----+</span><br><span class="line">| id | title       | lft | rgt |</span><br><span class="line">+----+-------------+-----+-----+</span><br><span class="line">|  <span class="number">1</span> | Smartphones |   <span class="number">1</span> |  <span class="number">14</span> |</span><br><span class="line">|  <span class="number">3</span> | Android     |   <span class="number">2</span> |   <span class="number">5</span> |</span><br><span class="line">|  <span class="number">4</span> | iOS         |   <span class="number">6</span> |   <span class="number">9</span> |</span><br><span class="line">|  <span class="number">5</span> | Harmony OS  |  <span class="number">10</span> |  <span class="number">13</span> |</span><br><span class="line">|  <span class="number">6</span> | 小米        |   <span class="number">3</span> |   <span class="number">4</span> |</span><br><span class="line">|  <span class="number">7</span> | iPhone      |   <span class="number">7</span> |   <span class="number">8</span> |</span><br><span class="line">|  <span class="number">8</span> | 华为        |  <span class="number">11</span> |  <span class="number">12</span> |</span><br><span class="line">+----+-------------+-----+-----+</span><br><span class="line">查看各个分类的级别</span><br><span class="line"></span><br><span class="line"> SELECT COUNT(c2.id) AS indentation, c1.title</span><br><span class="line">  FROM categories AS c1, categories AS c2</span><br><span class="line">  WHERE c1.lft BETWEEN c2.lft AND c2.rgt</span><br><span class="line">  GROUP BY c1.title</span><br><span class="line">  ORDER BY c1.lft;</span><br><span class="line"></span><br><span class="line">+-------------+-------------+</span><br><span class="line">| indentation | title       |</span><br><span class="line">+-------------+-------------+</span><br><span class="line">|           <span class="number">1</span> | Smartphones |</span><br><span class="line">|           <span class="number">2</span> | Android     |</span><br><span class="line">|           <span class="number">3</span> | 小米        |</span><br><span class="line">|           <span class="number">2</span> | iOS         |</span><br><span class="line">|           <span class="number">3</span> | iPhone      |</span><br><span class="line">|           <span class="number">2</span> | Harmony OS  |</span><br><span class="line">|           <span class="number">3</span> | 华为        |</span><br><span class="line">+-------------+-------------+</span><br><span class="line">https:<span class="comment">//learnku.com/articles/33630</span></span><br></pre></td></tr></table></figure>
<h3 id="冬令时"><a href="#冬令时" class="headerlink" title="冬令时"></a>冬令时</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getSeason</span>(<span class="params">$timezone</span>)</span>&#123;</span><br><span class="line">    $localzone = date(<span class="string">"e"</span>);</span><br><span class="line">    date_default_timezone_set($timezone);</span><br><span class="line">    $season = date(<span class="string">"I"</span>);</span><br><span class="line">    date_default_timezone_set($localzone);</span><br><span class="line">    <span class="keyword">return</span> $season;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">is_dst</span>(<span class="params">$timestamp,$timezone</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $timezone = date(<span class="string">'e'</span>); <span class="comment">//获取当前使用的时区</span></span><br><span class="line">    date_default_timezone_set($timezone); <span class="comment">//强制设置时区</span></span><br><span class="line">    $dst = date(<span class="string">'I'</span>,$timestamp); <span class="comment">//判断是否夏令时</span></span><br><span class="line">    date_default_timezone_set($timezone); <span class="comment">//还原时区</span></span><br><span class="line">    <span class="keyword">return</span> $dst; <span class="comment">//返回结果</span></span><br><span class="line">&#125;</span><br><span class="line">print_r(is_dst(<span class="number">1570143661</span>,<span class="string">'Europe/London'</span>));</span><br><span class="line">print_r(is_dst(time(),<span class="string">'America/New_York'</span>));</span><br><span class="line">print_r(getSeason(<span class="string">'America/Chicago'</span>));</span><br><span class="line">https:<span class="comment">//3v4l.org/iWVMb</span></span><br></pre></td></tr></table></figure>
<h3 id="展开表达式"><a href="#展开表达式" class="headerlink" title="展开表达式"></a>展开表达式</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&lt;?php</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">add</span>(<span class="params">$a, $b, $c</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> $a + $b + $c;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$operators = [<span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line">echo add(<span class="number">1</span>, ...$operators);</span><br><span class="line">?&gt;</span><br><span class="line">https:<span class="comment">//www.php.net/manual/zh/migration56.new-features.php</span></span><br></pre></td></tr></table></figure>
<h3 id="helper函数"><a href="#helper函数" class="headerlink" title="helper函数"></a>helper函数</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">register</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    foreach (glob(app_path(<span class="string">'Helpers'</span>) . <span class="string">'/*.php'</span>) <span class="keyword">as</span> $file) &#123;</span><br><span class="line">        require_once $file;</span><br><span class="line">    &#125;</span><br><span class="line">     $<span class="keyword">this</span>-&gt;directoryToArray(app_path(<span class="string">'Helpers'</span>));</span><br><span class="line">&#125;</span><br><span class="line">private <span class="function"><span class="keyword">function</span> <span class="title">directoryToArray</span>(<span class="params">$directory, $recursive = false</span>) </span>&#123;</span><br><span class="line">  $array_items = array();</span><br><span class="line">  <span class="keyword">if</span>(!is_dir($directory)) <span class="keyword">return</span> <span class="string">"$directory folder does not exist"</span>;</span><br><span class="line">      <span class="keyword">if</span> ($handle = opendir($directory)) &#123;</span><br><span class="line">          <span class="keyword">while</span> (<span class="literal">false</span> !== ($file = readdir($handle))) &#123;</span><br><span class="line">              <span class="keyword">if</span> ($file != <span class="string">"."</span> &amp;&amp; $file != <span class="string">".."</span>) &#123;</span><br><span class="line">                  <span class="keyword">if</span> (is_dir($directory. <span class="string">"/"</span> . $file)) &#123;</span><br><span class="line">                      <span class="keyword">if</span>($recursive) &#123;</span><br><span class="line">                          self::directoryToArray($directory. <span class="string">"/"</span> . $file, $recursive);</span><br><span class="line">                      &#125;</span><br><span class="line">                      <span class="keyword">if</span>($<span class="keyword">this</span>-&gt;validateExt($file))&#123;</span><br><span class="line">                          $file = $directory . <span class="string">"/"</span> . $file;</span><br><span class="line">                          require_once $file;</span><br><span class="line">                      &#125;</span><br><span class="line">              &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                     <span class="keyword">if</span>($<span class="keyword">this</span>-&gt;validateExt($file))&#123;</span><br><span class="line">                          $file = $directory . <span class="string">"/"</span> . $file;</span><br><span class="line">                          require_once $file;</span><br><span class="line">                      &#125;</span><br><span class="line">              &#125;</span><br><span class="line">         &#125;</span><br><span class="line">     &#125;</span><br><span class="line">     closedir($handle);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">private <span class="function"><span class="keyword">function</span> <span class="title">validateExt</span>(<span class="params">$file</span>)</span>&#123;</span><br><span class="line">  $file_path = pathinfo($file);</span><br><span class="line">  <span class="keyword">if</span>($file_path [<span class="string">'extension'</span>] == <span class="string">'php'</span>)&#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">  &#125;</span><br><span class="line">      <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br><span class="line">config/app.php</span><br><span class="line">App\Providers\HelperServiceProvider::<span class="class"><span class="keyword">class</span>,</span></span><br><span class="line">App\Providers\AppServiceProvider::class,</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">carbon</span>(<span class="params">$time = null, $tz = null</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> \Carbon\Carbon($time, $tz);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="魔术方法-call"><a href="#魔术方法-call" class="headerlink" title="魔术方法__call"></a>魔术方法__call</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Family</span></span>&#123;</span><br><span class="line">            protected $childName = [];</span><br><span class="line">            public <span class="function"><span class="keyword">function</span> <span class="title">fatherName</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="string">'Dale'</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            public <span class="function"><span class="keyword">function</span> <span class="title">motherName</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="string">'Kelly'</span>;                </span><br><span class="line">            &#125;</span><br><span class="line">            public <span class="function"><span class="keyword">function</span> <span class="title">__call</span>(<span class="params">$name,$param</span>)</span>&#123;</span><br><span class="line">                <span class="keyword">if</span>($name == <span class="string">'setChildName'</span>)&#123;</span><br><span class="line">                        $childNmae = [$param];</span><br><span class="line">                        <span class="keyword">return</span> $<span class="keyword">this</span> </span><br><span class="line">                    &#125;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">     &#125;</span><br><span class="line">     $family = <span class="keyword">new</span> Family();</span><br><span class="line">     $setChildName = $family-&gt;setChildName(<span class="string">'Kobe'</span>);</span><br><span class="line">在使用服务容器时，我们的目标是以下面形式调用我的方法。</span><br><span class="line"></span><br><span class="line">$myContainer = app(<span class="string">'myContainer'</span>);</span><br><span class="line">$myContainer-&gt;myClass()-&gt;myFunction();</span><br><span class="line">当然我可以用构造函数的方法来解决，但是当依赖了很多类的时候，构造函数是毁灭性的。调用一个方法会实例化N个类，十分不可取。所以我改造myClass如下。</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">myClass</span></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__call</span>(<span class="params">$name, $arguments</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $className = <span class="string">'App\Container\\'</span>.$name;</span><br><span class="line">        <span class="keyword">if</span> (class_exists($className))&#123;</span><br><span class="line">            $<span class="class"><span class="keyword">class</span> </span>= <span class="keyword">new</span> \ReflectionClass($className);</span><br><span class="line">            <span class="keyword">return</span> $<span class="class"><span class="keyword">class</span>-&gt;<span class="title">newInstanceArgs</span>($<span class="title">arguments</span>)</span>;</span><br><span class="line">        &#125;    </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">这样会在调用的时候，只会实例化一个类，而且代码简洁明了https:<span class="comment">//dalebao.github.io/2019/04/27/Laravel-and-magic/</span></span><br></pre></td></tr></table></figure>
<h3 id="获取每年所有周"><a href="#获取每年所有周" class="headerlink" title="获取每年所有周"></a>获取每年所有周</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">get_week</span>(<span class="params">$year</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $year_start = $year . <span class="string">"-01-01"</span>;</span><br><span class="line">    $year_end = $year . <span class="string">"-12-31"</span>;</span><br><span class="line">    $startday = strtotime($year_start);</span><br><span class="line">    <span class="keyword">if</span> (intval(date(<span class="string">'N'</span>, $startday)) != <span class="string">'1'</span>) &#123;</span><br><span class="line">        $startday = strtotime(<span class="string">"next monday"</span>, strtotime($year_start)); <span class="comment">//获取年第一周的日期</span></span><br><span class="line">    &#125;</span><br><span class="line">    $year_mondy = date(<span class="string">"Y-m-d"</span>, $startday); <span class="comment">//获取年第一周的日期</span></span><br><span class="line"></span><br><span class="line">    $endday = strtotime($year_end);</span><br><span class="line">    <span class="keyword">if</span> (intval(date(<span class="string">'W'</span>, $endday)) == <span class="string">'7'</span>) &#123;</span><br><span class="line">        $endday = strtotime(<span class="string">"last sunday"</span>, strtotime($year_end));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//如果是当前年则统计到当前周</span></span><br><span class="line">    $now_year = date(<span class="string">'Y'</span>);</span><br><span class="line">    <span class="keyword">if</span> ($now_year == $year) &#123;</span><br><span class="line">        $num = date(<span class="string">'W'</span>, strtotime(date(<span class="string">'Y-m-d'</span>)));</span><br><span class="line">    &#125;  <span class="keyword">else</span> &#123;</span><br><span class="line">        $num = intval(date(<span class="string">'W'</span>, $endday));</span><br><span class="line">        <span class="keyword">if</span> ($num == <span class="string">'1'</span>)&#123;</span><br><span class="line">            $num = intval(date(<span class="string">'W'</span>,$endday -(<span class="number">7</span>*<span class="number">24</span>*<span class="number">3600</span>)))+<span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> ($i = <span class="number">1</span>; $i &lt;= $num; $i++) &#123;</span><br><span class="line">        $j = $i <span class="number">-1</span>;</span><br><span class="line">        $start_date = date(<span class="string">"Y-m-d"</span>, strtotime(<span class="string">"$year_mondy $j week "</span>));</span><br><span class="line">        $end_day = date(<span class="string">"Y-m-d"</span>, strtotime(<span class="string">"$start_date +6 day"</span>));</span><br><span class="line">        <span class="keyword">if</span> (!(date(<span class="string">"Y"</span>,strtotime(<span class="string">"$year_mondy $j week "</span>)) &gt; $year))&#123;</span><br><span class="line">            $week_array[$i] = array(</span><br><span class="line">                substr(str_replace(<span class="string">"-"</span>, <span class="string">"."</span>, $start_date), <span class="number">5</span>),</span><br><span class="line">                substr(str_replace(<span class="string">"-"</span>, <span class="string">"."</span>, $end_day), <span class="number">5</span>));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> $week_array;</span><br><span class="line">&#125;https:<span class="comment">//dalebao.github.io/2019/04/27/%E8%8E%B7%E5%8F%96%E6%AF%8F%E5%B9%B4%E6%89%80%E6%9C%89%E5%91%A8/</span></span><br></pre></td></tr></table></figure>
<h3 id="php-yield"><a href="#php-yield" class="headerlink" title="php yield"></a>php yield</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"> public <span class="function"><span class="keyword">function</span> <span class="title">parseStr</span>(<span class="params">...$param</span>)</span>&#123;</span><br><span class="line">        $str = <span class="string">''</span>;</span><br><span class="line">        foreach ($param <span class="keyword">as</span> $key =&gt; $value) &#123;</span><br><span class="line">            <span class="comment">//处理数组</span></span><br><span class="line">            <span class="keyword">if</span> (is_array($value)) &#123;</span><br><span class="line">                $value = md5(json_encode($value));</span><br><span class="line">            &#125;</span><br><span class="line">            $str .= $value;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $str;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">handle</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// 单独输出到updateChapter.log文件</span></span><br><span class="line">        \Log::useFiles(storage_path().<span class="string">'/logs/data.log'</span>);</span><br><span class="line">        $iTime = time();</span><br><span class="line">        \Log::info(<span class="string">'start'</span>, [<span class="string">'timestamp'</span> =&gt; $iTime]);</span><br><span class="line">        $i = <span class="number">0</span>;</span><br><span class="line">        $ret =  $<span class="keyword">this</span>-&gt;traverse();</span><br><span class="line">        foreach ($ret <span class="keyword">as</span> $v) &#123;</span><br><span class="line">            foreach ($v <span class="keyword">as</span> $value) &#123;</span><br><span class="line">                $key = $<span class="keyword">this</span>-&gt;parseStr(<span class="string">'wx:'</span>, $value-&gt;wx_id);</span><br><span class="line">                $count = (int) Redis::pfcount($key);</span><br><span class="line">                <span class="comment">// redis里的uv更大时更新，并记录更新前后数据</span></span><br><span class="line">                <span class="keyword">if</span> ($count &gt; $value-&gt;uv) &#123;</span><br><span class="line">                    \Log::info(<span class="string">'update id:'</span>.$value-&gt;id, [$value-&gt;<span class="function"><span class="params">uv</span> =&gt;</span> $count]);</span><br><span class="line">                    $i++;</span><br><span class="line">                    (<span class="keyword">new</span> StatisticModel([<span class="string">'hash_id'</span> =&gt; $value-&gt;wx_id]))-&gt;where(<span class="string">'id'</span>, $value-&gt;id)-&gt;update([<span class="string">'uv'</span> =&gt; $count, <span class="string">'update_time'</span> =&gt; $iTime]);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 记录更新时间与数量</span></span><br><span class="line">        \Log::info(<span class="string">'end'</span>, [<span class="string">'timestamp'</span>=&gt;time(), <span class="string">'count'</span> =&gt; $i,<span class="string">'interval'</span> =&gt; time() - $iTime]);</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private <span class="function"><span class="keyword">function</span> <span class="title">traverse</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $page = <span class="number">3000</span>;</span><br><span class="line">        <span class="keyword">for</span> ($i = <span class="number">1</span>; $i &lt; <span class="number">6</span>; $i++) &#123;</span><br><span class="line">            $count = DB::table(<span class="string">'wx_statistics_'</span>.$i)-&gt;count();</span><br><span class="line">            <span class="keyword">if</span> ($count &lt; <span class="number">1</span>) &#123;</span><br><span class="line">                <span class="comment">// 记录进度</span></span><br><span class="line">                \Log::info(<span class="string">'update schedule'</span>, [<span class="string">'table success'</span> =&gt; $i]);</span><br><span class="line">                <span class="keyword">continue</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            $num = ceil($count/$page);</span><br><span class="line">            <span class="keyword">for</span> ($j = <span class="number">0</span>; $j &lt; $num; $j++) &#123;</span><br><span class="line">                $ret = DB::table(<span class="string">'wx_statistics_'</span>.$i)</span><br><span class="line">                    -&gt;select([<span class="string">'id'</span>, <span class="string">'wx_id'</span>, <span class="string">'uv'</span>])</span><br><span class="line">                    -&gt;orderBy(<span class="string">'id'</span>, <span class="string">'asc'</span>)</span><br><span class="line">                    -&gt;skip($j*$page)</span><br><span class="line">                    -&gt;take($page)</span><br><span class="line">                    -&gt;get()</span><br><span class="line">                    -&gt;toArray();</span><br><span class="line"></span><br><span class="line">                <span class="keyword">if</span> (empty($ret)) &#123;</span><br><span class="line">                    <span class="keyword">continue</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">yield</span> $ret;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            \Log::info(<span class="string">'update schedule'</span>, [<span class="string">'table success'</span> =&gt; $i]);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">http:<span class="comment">//blog.13sai.com/essay/199</span></span><br></pre></td></tr></table></figure>
<h3 id="PHP-规范-Symfony-代码规范"><a href="#PHP-规范-Symfony-代码规范" class="headerlink" title="PHP 规范 - Symfony 代码规范"></a>PHP 规范 - Symfony 代码规范</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">使用 php-cs-fixer 工具来自动检查编码规范</span><br><span class="line"></span><br><span class="line">$ cd your-project/</span><br><span class="line">$ php php-cs-fixer.phar fix -v</span><br><span class="line">https:<span class="comment">//learnku.com/articles/36079</span></span><br></pre></td></tr></table></figure>
<h3 id="Laravel-限制条数后再分页"><a href="#Laravel-限制条数后再分页" class="headerlink" title="Laravel 限制条数后再分页"></a>Laravel 限制条数后再分页</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">$per_page = <span class="number">15</span>; <span class="comment">// 每页条数</span></span><br><span class="line">$limit_page = <span class="number">30</span>; <span class="comment">// 限制总页数</span></span><br><span class="line">$data = Article::orderBy(<span class="string">'id'</span>, <span class="string">'desc'</span>)-&gt;limit($per_page * $limit_page)-&gt;paginate($per_page);</span><br><span class="line">此种写法并不能生效，因为 paginate 方法中内置的 limit 会替换掉我们的 limit</span><br><span class="line">取出要展示的条数的最小 id</span><br><span class="line"></span><br><span class="line">在总记录中设置 id 大于上一条件得到的最小 id</span><br><span class="line"></span><br><span class="line">即，用 where 替换 limit 限制总条数</span><br><span class="line"></span><br><span class="line">$last_id = Article::orderBy(<span class="string">'id'</span>, <span class="string">'desc'</span>)</span><br><span class="line">    -&gt;limit($per_page * $limit_page)</span><br><span class="line">    -&gt;pluck(<span class="string">'id'</span>)</span><br><span class="line">    -&gt;sort()</span><br><span class="line">    -&gt;first();</span><br><span class="line">$data = Article::orderBy(<span class="string">'id'</span>, <span class="string">'desc'</span>)</span><br><span class="line">    -&gt;where(<span class="string">'id'</span>, <span class="string">'&gt;'</span>, $last_id)</span><br><span class="line">    -&gt;paginate($per_page);</span><br><span class="line">https:<span class="comment">//learnku.com/articles/36260</span></span><br></pre></td></tr></table></figure>
<h3 id="php-ini-配置"><a href="#php-ini-配置" class="headerlink" title="php.ini 配置"></a>php.ini 配置</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">文件上传</span><br><span class="line">file_uploads = On</span><br><span class="line">max_file_uploads = <span class="number">20</span></span><br><span class="line">upload_max_filesize = <span class="number">2</span>M</span><br><span class="line">max_execution_time = <span class="number">30</span> 值 为 <span class="number">0</span> 代表没有限制</span><br></pre></td></tr></table></figure>
<h3 id="PHP-Traits"><a href="#PHP-Traits" class="headerlink" title="PHP Traits"></a>PHP Traits</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">“ Trait 是单继承语言（如 PHP ）中的一种代码复用机制。 Trai 的目的是减少单继承语言的一些限制，能让开发者自由的重用在不同的类层次结构下几个独立类中的方法。 Trait 的语义组合与类的定义在某种程度上减少了代码的复杂度，避免了与多继承和 Mixins 相关的一些典型问题。</span><br><span class="line"></span><br><span class="line">Trait 与类非常相似，但它的目的仅仅是用更好、一致的方式汇聚一些方法。 Trait 本身不能被实例化。除了传统继承之外，它能水平组合行为，换言之，类的应用程序不需要继承。”</span><br><span class="line">namespace App\Http\Traits;</span><br><span class="line"></span><br><span class="line">use App\Brand;</span><br><span class="line">trait BrandsTrait &#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">brandsAll</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="comment">// 从品牌表中获取所有品牌.</span></span><br><span class="line">        $brands = Brand::all();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $brands;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">use App\Http\Traits\BrandsTrait;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">YourController</span> <span class="keyword">extends</span> <span class="title">Controller</span> </span>&#123;</span><br><span class="line">        use BrandsTrait;</span><br><span class="line"></span><br><span class="line">        public <span class="function"><span class="keyword">function</span> <span class="title">addProduct</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">            $brands = $<span class="keyword">this</span>-&gt;brandsAll();</span><br><span class="line">        &#125;</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/laravel/t/36211</span></span><br></pre></td></tr></table></figure>
<h3 id="正则表达式"><a href="#正则表达式" class="headerlink" title="正则表达式"></a>正则表达式</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">给定一组手机号，必须由[0123456789]组成的，才选出来从哪找?从字符串的开始找，找到字符串的结束 ^ $ </span></span><br><span class="line"><span class="comment">找谁[01235689]</span></span><br><span class="line"><span class="comment">找几个?11个</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line">$arr = array(<span class="string">'13800138000'</span>，<span class="string">'13487656887'</span>，<span class="string">'434456'</span>，<span class="string">'45454353434543'</span>);</span><br><span class="line"><span class="comment">//$patt = '/^[^47]&#123;11&#125;$/'; //补集方法</span></span><br><span class="line">$patt = <span class="string">'/^[01235689]&#123;11&#125;$/'</span>;<span class="comment">//集合方式</span></span><br><span class="line">foreach($arr <span class="keyword">as</span> $v)&#123;</span><br><span class="line">    preg_match_all($patt，$v，$res);</span><br><span class="line">    print_r($res);</span><br><span class="line">&#125;</span><br><span class="line"> $str = <span class="string">'tommorw is another day，o2o ，you dont bird me i dont bird you'</span>;</span><br><span class="line"></span><br><span class="line">&gt;&gt;&gt; preg_split(<span class="string">'#\W+#'</span>,$str)</span><br><span class="line">=&gt; [</span><br><span class="line">     <span class="string">"tommorw"</span>,</span><br><span class="line">     <span class="string">"is"</span>,</span><br><span class="line">     <span class="string">"another"</span>,</span><br><span class="line">     <span class="string">"day"</span>,</span><br><span class="line">     <span class="string">"o2o"</span>,</span><br><span class="line">     <span class="string">"you"</span>,</span><br><span class="line">     <span class="string">"dont"</span>,</span><br><span class="line">     <span class="string">"bird"</span>,</span><br><span class="line">     <span class="string">"me"</span>,</span><br><span class="line">     <span class="string">"i"</span>,</span><br><span class="line">     <span class="string">"dont"</span>,</span><br><span class="line">     <span class="string">"bird"</span>,</span><br><span class="line">     <span class="string">"you"</span>,</span><br><span class="line">   ]</span><br><span class="line"><span class="comment">//查询纯数字或者纯字母的词</span></span><br><span class="line">$str = <span class="string">'hello o2o 2b9 250'</span>;</span><br><span class="line">$patt = <span class="string">'/\b[a-zA-Z]+\b|\b[0-9]+\b/'</span>;<span class="comment">//最少一个</span></span><br><span class="line">preg_match_all($patt，$str，$res);</span><br><span class="line">print_r($res);</span><br><span class="line"></span><br><span class="line"><span class="comment">//查询苹果系统的产品</span></span><br><span class="line">$str = <span class="string">'ipad，iphone，imac，ipod，iamsorry'</span>;</span><br><span class="line">$patt = <span class="string">'/\bi(pad|phone|mac|pod)\b/'</span>;</span><br><span class="line">preg_match_all($patt，$str，$res);</span><br><span class="line">print_r($res);</span><br><span class="line">把手机号中间的 <span class="number">4</span> 位替换为 *</span><br><span class="line"></span><br><span class="line">$str = <span class="string">'13800138000 , 13426060134 '</span>;</span><br><span class="line"><span class="comment">//前3位和后4位放子表达式中，中间4位随便，保留子表达式.替换中间的4位</span></span><br><span class="line">$patt = <span class="string">'/(\d&#123;3&#125;)\d&#123;4&#125;(\d&#123;4&#125;)/'</span>;</span><br><span class="line"><span class="comment">//preg_match_all($patt,$str, $res);</span></span><br><span class="line"><span class="comment">//print_r($res);</span></span><br><span class="line">echo preg_replace($patt， <span class="string">'\1****\2'</span>， $str);<span class="comment">//138****8000 , 134****0134</span></span><br><span class="line">$str = <span class="string">'bob李'</span>;</span><br><span class="line">$patt = <span class="string">'/^[\x&#123;4e00&#125;-\x&#123;9fa5&#125;]+$/u'</span>;</span><br><span class="line">echo preg_match($patt，$str)?<span class="string">'国货'</span>:<span class="string">'杂货'</span>;https:<span class="comment">//learnku.com/articles/36227</span></span><br></pre></td></tr></table></figure>
<h3 id="curl-下载文件"><a href="#curl-下载文件" class="headerlink" title="curl 下载文件"></a>curl 下载文件</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">curl_download_file</span>(<span class="params">$url,$path</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $ch=curl_init();</span><br><span class="line">    curl_setopt($ch,CURLOPT_POST,<span class="number">0</span>);</span><br><span class="line">    curl_setopt($ch,CURLOPT_URL,$url);</span><br><span class="line">    curl_setopt($ch,CURLOPT_RETURNTRANSFER,<span class="number">1</span>);</span><br><span class="line">    $content=curl_exec($ch);</span><br><span class="line"><span class="comment">//    file_put_contents($path, $content);</span></span><br><span class="line"><span class="comment">//    $file=fopen($path,'w');</span></span><br><span class="line"><span class="comment">//    fwrite($file,$content);</span></span><br><span class="line"><span class="comment">//    fclose($file);</span></span><br><span class="line">    header(<span class="string">'Expires: 0'</span>); <span class="comment">// no cache</span></span><br><span class="line">    header(<span class="string">'Cache-Control: must-revalidate, post-check=0, pre-check=0'</span>);</span><br><span class="line">    header(<span class="string">'Last-Modified: '</span> . gmdate(<span class="string">'D, d M Y H:i:s'</span>, time()) . <span class="string">' GMT'</span>);</span><br><span class="line">    header(<span class="string">'Cache-Control: private'</span>, <span class="literal">false</span>);</span><br><span class="line">    header(<span class="string">'Content-Type: application/force-download'</span>);</span><br><span class="line">    header(<span class="string">'Content-Disposition: attachment; filename="'</span> . basename($url) . <span class="string">'"'</span>);</span><br><span class="line">    header(<span class="string">'Content-Transfer-Encoding: binary'</span>);</span><br><span class="line">    header(<span class="string">'Content-Length: '</span> . strlen($content)); <span class="comment">// provide file size</span></span><br><span class="line">    header(<span class="string">'Connection: close'</span>);</span><br><span class="line">    echo $content;</span><br><span class="line">&#125;</span><br><span class="line">curl_download_file(<span class="string">'https://www1.hkexnews.hk/listedco/listconews/sehk/2019/1108/2019110800352_c.xlsx'</span>,<span class="string">'test.xlsx'</span>);</span><br></pre></td></tr></table></figure>
<h3 id="更新数据后返回原来数据"><a href="#更新数据后返回原来数据" class="headerlink" title="更新数据后返回原来数据"></a>更新数据后返回原来数据</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"> $article = Finance::where(<span class="string">'state'</span>, <span class="number">0</span>)</span><br><span class="line">                            -&gt;orderBy(<span class="string">'id'</span>, <span class="string">'asc'</span>)-&gt;select(<span class="string">'id'</span>, <span class="string">'state'</span>, <span class="string">'author'</span>)</span><br><span class="line">                            -&gt;first();</span><br><span class="line"></span><br><span class="line">        $old_article = $article-&gt;toArray();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span>($article)&#123;</span><br><span class="line">            $article-&gt;state = <span class="number">1</span>;</span><br><span class="line">            $article-&gt;author = <span class="string">'elesos'</span><span class="string">';</span></span><br><span class="line"><span class="string">            $article-&gt;save();</span></span><br><span class="line"><span class="string">        &#125;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">        return $old_article;</span></span><br><span class="line"><span class="string">当把一个对象已经创建的实例赋给一个新变量时，新变量会访问同一个实例，就和用该对象赋值一样。此行为和给函数传递入实例时一样。可以用克隆给一个已创建的对象建立一个新实例。 </span></span><br><span class="line"><span class="string"> $old_article = $article = Finance::where('</span>state<span class="string">', 0)</span></span><br><span class="line"><span class="string">                            -&gt;orderBy('</span>id<span class="string">', '</span>asc<span class="string">')</span></span><br><span class="line"><span class="string">                            -&gt;first();</span></span><br><span class="line"><span class="string">old_article 和 article 都是指向同一块内存（或者说同一个对象实例。从 C 语言的角度理解，可以类比为他们都是保存指向同一个对象的 内存地址，即指针），那么对于 article 的修改自然也会反应到 old_article 上</span></span><br><span class="line"><span class="string">$article = Finance::where('</span>state<span class="string">', 0)</span></span><br><span class="line"><span class="string">                            -&gt;orderBy('</span>id<span class="string">', '</span>asc<span class="string">')</span></span><br><span class="line"><span class="string">                            -&gt;first();</span></span><br><span class="line"><span class="string">// 关键：复制 article 实例到 old_article。</span></span><br><span class="line"><span class="string">// 若支持，此时 old_article 指向的实例与 article 不一致</span></span><br><span class="line"><span class="string">$old_article = clone $article;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">$article-&gt;state = 1;</span></span><br><span class="line"><span class="string">$article-&gt;save();</span></span><br><span class="line"><span class="string">// 如果证实可以，此处 old_article state 应为 0</span></span><br><span class="line"><span class="string">echo $old_article-&gt;state;</span></span><br><span class="line"><span class="string">https://learnku.com/laravel/t/36338</span></span><br></pre></td></tr></table></figure>
<h3 id="生成器"><a href="#生成器" class="headerlink" title="生成器"></a>生成器</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line">最普通的方式就是一次性读取文件内容，然后再进行遍历。</span><br><span class="line"></span><br><span class="line">&lt;?php</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getLinesFromFile</span>(<span class="params">$fileName</span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 打开文件</span></span><br><span class="line">    <span class="keyword">if</span> (!$fileHandle = fopen($fileName, <span class="string">'r'</span>)) &#123;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 一次读取每一行并保存</span></span><br><span class="line">    $lines = [];</span><br><span class="line">    <span class="keyword">while</span> (<span class="literal">false</span> !== $line = fgets($fileHandle)) &#123;</span><br><span class="line">        $lines[] = $line;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    fclose($fileHandle);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> $lines;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$lines = getLinesFromFile(<span class="string">'test.txt'</span>);</span><br><span class="line">foreach ($lines <span class="keyword">as</span> $line) &#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line">当使用该函数读取大文件时，就会因为内存不足而报错。</span><br><span class="line"></span><br><span class="line">PHP Fatal error:  Allowed memory size <span class="keyword">of</span> <span class="number">134217728</span> bytes exhausted</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getLinesFromFile</span>(<span class="params">$fileName</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (!$fileHandle = fopen($fileName, <span class="string">'r'</span>)) &#123;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> (<span class="literal">false</span> !== $line = fgets($fileHandle)) &#123;</span><br><span class="line">        <span class="keyword">yield</span> $line;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    fclose($fileHandle);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$lines = getLinesFromFile(<span class="string">'test.txt'</span>);</span><br><span class="line">foreach ($lines <span class="keyword">as</span> $line) &#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">yield</span>;</span><br><span class="line"><span class="keyword">yield</span> $value;</span><br><span class="line"><span class="keyword">yield</span> $key =&gt; $value;</span><br><span class="line">生成器看上去是函数，实际上是 Generator 类的实例。</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">simpleGenerator</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;   </span><br><span class="line">    <span class="keyword">yield</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">echo get_class(simpleGenerator())  <span class="comment">// Generator</span></span><br><span class="line">既然是对象，就可以将其赋值给变量。</span><br><span class="line"></span><br><span class="line">$gen = simpleGenerator();</span><br><span class="line">Generator 对象已经实现了 Iterator 接口</span><br><span class="line"></span><br><span class="line">$gen <span class="keyword">instanceof</span> Iterator <span class="comment">// true</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">echoLogger</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">while</span> (<span class="literal">true</span>) &#123;</span><br><span class="line">        <span class="comment">// 接受外部的传值</span></span><br><span class="line">        $log = <span class="keyword">yield</span>;</span><br><span class="line">        echo <span class="string">'Log: '</span> . $log . <span class="string">"\n"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$logger = echoLogger();</span><br><span class="line">$logger-&gt;send(<span class="string">'Foo'</span>);  <span class="comment">// Log: foo</span></span><br><span class="line">$logger-&gt;send(<span class="string">'Bar'</span>);  <span class="comment">// Log: bar</span></span><br><span class="line">https:<span class="comment">//learnku.com/articles/36331</span></span><br></pre></td></tr></table></figure>
<h3 id="多维数组变一维"><a href="#多维数组变一维" class="headerlink" title="多维数组变一维"></a>多维数组变一维</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"> public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">flatten</span>(<span class="params">$array, $depth = INF</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $result = [];</span><br><span class="line">        foreach ($array <span class="keyword">as</span> $item) &#123;</span><br><span class="line">            $item = $item <span class="keyword">instanceof</span> Collection ? $item-&gt;all() : $item;</span><br><span class="line">            <span class="keyword">if</span> (! is_array($item)) &#123;</span><br><span class="line">                $result[] = $item;</span><br><span class="line">            &#125; elseif ($depth === <span class="number">1</span>) &#123;</span><br><span class="line">                $result = array_merge($result, array_values($item));</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                $result = array_merge($result, <span class="attr">static</span>::flatten($item, $depth - <span class="number">1</span>));</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> $result;</span><br><span class="line">    &#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/36348</span></span><br></pre></td></tr></table></figure>
<h3 id="phpexcel遇到内存溢出"><a href="#phpexcel遇到内存溢出" class="headerlink" title="phpexcel遇到内存溢出"></a>phpexcel遇到内存溢出</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ExportService</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line"></span><br><span class="line">    public <span class="keyword">static</span> $outPutFile = <span class="string">''</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 导出文件</span></span><br><span class="line"><span class="comment">     * @param string $fileName</span></span><br><span class="line"><span class="comment">     * @param $data</span></span><br><span class="line"><span class="comment">     * @param array $formFields</span></span><br><span class="line"><span class="comment">     * @return mixed</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">exportData</span>(<span class="params">$fileName = <span class="string">''</span>, $data, $formFields = []</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $fileArr = [];</span><br><span class="line">        $tmpPath = \Yii::$app-&gt;params[<span class="string">'excelSavePath'</span>];</span><br><span class="line"></span><br><span class="line">        foreach (array_chunk($data, <span class="number">10000</span>) <span class="keyword">as</span> $key =&gt; $value) &#123;</span><br><span class="line">            self::$outPutFile = <span class="string">''</span>;</span><br><span class="line">            $subject          = !empty($fileName) ? $fileName : <span class="string">'data_'</span>;</span><br><span class="line">            $subject          .= date(<span class="string">'YmdHis'</span>);</span><br><span class="line">            <span class="keyword">if</span> (empty($value) || empty($formFields)) &#123;</span><br><span class="line">                <span class="keyword">continue</span>;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            self::$outPutFile = $tmpPath . $subject . $key . <span class="string">'.csv'</span>;</span><br><span class="line">            <span class="keyword">if</span> (!file_exists(self::$outPutFile)) &#123;</span><br><span class="line">                touch(self::$outPutFile);</span><br><span class="line">            &#125;</span><br><span class="line">            $index  = array_keys($formFields);</span><br><span class="line">            $header = array_values($formFields);</span><br><span class="line">            self::outPut($header);</span><br><span class="line"></span><br><span class="line">            foreach ($value <span class="keyword">as</span> $k =&gt; $v) &#123;</span><br><span class="line">                $tmpData = [];</span><br><span class="line">                foreach ($index <span class="keyword">as</span> $item) &#123;</span><br><span class="line">                    $tmpData[] = isset($v[$item]) ? $v[$item] : <span class="string">''</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                self::outPut($tmpData);</span><br><span class="line">            &#125;</span><br><span class="line">            $fileArr[] = self::$outPutFile;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        $zipFile = $tmpPath . $fileName . date(<span class="string">'YmdHi'</span>) . <span class="string">'.zip'</span>;</span><br><span class="line">        $zipRes = self::zipFile($fileArr, $zipFile);</span><br><span class="line">        <span class="keyword">return</span> $zipRes;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 向文件写入数据</span></span><br><span class="line"><span class="comment">     * @param array $data</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">outPut</span>(<span class="params">$data = []</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (is_array($data) &amp;&amp; !empty($data)) &#123;</span><br><span class="line">            $data = implode(<span class="string">','</span>, $data);</span><br><span class="line">            file_put_contents(self::$outPutFile, iconv(<span class="string">"UTF-8"</span>, <span class="string">"GB2312//IGNORE"</span>, $data) . PHP_EOL, FILE_APPEND);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 压缩文件</span></span><br><span class="line"><span class="comment">     * @param $sourceFile</span></span><br><span class="line"><span class="comment">     * @param $distFile</span></span><br><span class="line"><span class="comment">     * @return mixed</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">zipFile</span>(<span class="params">$sourceFile, $distFile</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $zip = <span class="keyword">new</span> \ZipArchive();</span><br><span class="line">        <span class="keyword">if</span> ($zip-&gt;open($distFile, \ZipArchive::CREATE) !== <span class="literal">true</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> $sourceFile;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        $zip-&gt;open($distFile, \ZipArchive::CREATE);</span><br><span class="line">        foreach ($sourceFile <span class="keyword">as</span> $file) &#123;</span><br><span class="line">            $fileContent = file_get_contents($file);</span><br><span class="line">            $file        = iconv(<span class="string">'utf-8'</span>, <span class="string">'GBK'</span>, basename($file));</span><br><span class="line">            $zip-&gt;addFromString($file, $fileContent);</span><br><span class="line">        &#125;</span><br><span class="line">        $zip-&gt;close();</span><br><span class="line">        <span class="keyword">return</span> $distFile;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">        <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 下载文件</span></span><br><span class="line"><span class="comment">     * @param $filePath</span></span><br><span class="line"><span class="comment">     * @param $fileName</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">download</span>(<span class="params">$filePath, $fileName</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (!file_exists($filePath . $fileName)) &#123;</span><br><span class="line">            header(<span class="string">'HTTP/1.1 404 NOT FOUND'</span>);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">//以只读和二进制模式打开文件</span></span><br><span class="line">            $file = fopen($filePath . $fileName, <span class="string">"rb"</span>);</span><br><span class="line"></span><br><span class="line">            <span class="comment">//告诉浏览器这是一个文件流格式的文件</span></span><br><span class="line">            Header(<span class="string">"Content-type: application/octet-stream"</span>);</span><br><span class="line">            <span class="comment">//请求范围的度量单位</span></span><br><span class="line">            Header(<span class="string">"Accept-Ranges: bytes"</span>);</span><br><span class="line">            <span class="comment">//Content-Length是指定包含于请求或响应中数据的字节长度</span></span><br><span class="line">            Header(<span class="string">"Accept-Length: "</span> . filesize($filePath . $fileName));</span><br><span class="line">            <span class="comment">//用来告诉浏览器，文件是可以当做附件被下载，下载后的文件名称为$file_name该变量的值</span></span><br><span class="line">            Header(<span class="string">"Content-Disposition: attachment; filename="</span> . $fileName);</span><br><span class="line"></span><br><span class="line">            <span class="comment">//读取文件内容并直接输出到浏览器</span></span><br><span class="line">            echo fread($file, filesize($filePath . $fileName));</span><br><span class="line">            fclose($file);</span><br><span class="line">            exit();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;		$fileName = <span class="string">"库存导入模板"</span>;</span><br><span class="line">        $stockRes = []; <span class="comment">// 导出的数据</span></span><br><span class="line">        $formFields = [</span><br><span class="line">            <span class="string">'store_id'</span>  =&gt; <span class="string">'门店ID'</span>,</span><br><span class="line">            <span class="string">'storeName'</span> =&gt; <span class="string">'门店名称'</span>,</span><br><span class="line">            <span class="string">'sku'</span>       =&gt; <span class="string">'SKU编码'</span>,</span><br><span class="line">            <span class="string">'name'</span>      =&gt; <span class="string">'SKU名称'</span>,</span><br><span class="line">            <span class="string">'stock'</span>     =&gt; <span class="string">'库存'</span>,</span><br><span class="line">            <span class="string">'reason'</span>    =&gt; <span class="string">'原因'</span></span><br><span class="line">        ];</span><br><span class="line">        $fileRes    = ExportService::exportData($fileName, $stockRes, $formFields);</span><br><span class="line">        $tmpPath    = \Yii::$app-&gt;params[<span class="string">'excelSavePath'</span>]; <span class="comment">// 文件路径</span></span><br><span class="line">        $fileName   = str_replace($tmpPath, <span class="string">''</span>, $fileRes);</span><br><span class="line">        </span><br><span class="line">        <span class="comment">// 下载文件</span></span><br><span class="line">        ExportService::download($tmpPath, $fileName);</span><br><span class="line">        https:<span class="comment">//tsmliyun.github.io/php/%E5%85%B3%E4%BA%8EPHP%E5%86%85%E5%AD%98%E6%BA%A2%E5%87%BA%E7%9A%84%E6%80%9D%E8%80%83/</span></span><br></pre></td></tr></table></figure>
<h3 id="基于雪花算法的-PHP-ID-生成器"><a href="#基于雪花算法的-PHP-ID-生成器" class="headerlink" title="基于雪花算法的 PHP ID 生成器"></a>基于雪花算法的 PHP ID 生成器</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line">第一个 bit 为未使用的符号位。</span><br><span class="line">第二部分由 <span class="number">41</span> 位的时间戳（毫秒）构成，他的取值是当前时间相对于某一时间的偏移量。</span><br><span class="line">第三部分和第四部分的 <span class="number">5</span> 个 bit 位表示数据中心和机器 ID，其能表示的最大值为 <span class="number">2</span>^<span class="number">5</span> <span class="number">-1</span> = <span class="number">31</span>；</span><br><span class="line">最后部分由 <span class="number">12</span> 个 bit 组成，其表示每个工作节点每毫秒生成的序列号 ID，同一毫秒内最多可生成 <span class="number">2</span>^<span class="number">12</span> <span class="number">-1</span> 即 <span class="number">4095</span> 个 ID。</span><br><span class="line">composer <span class="built_in">require</span> godruoyi/php-snowflake</span><br><span class="line">$snowflake = <span class="keyword">new</span> \Godruoyi\Snowflake\Snowflake;</span><br><span class="line"></span><br><span class="line">$snowflake-&gt;id();</span><br><span class="line"><span class="comment">// 1537200202186752</span></span><br><span class="line">$snowflake = <span class="keyword">new</span> \Godruoyi\Snowflake\Snowflake;</span><br><span class="line">$snowflake-&gt;setStartTimeStamp(strtotime(<span class="string">'2019-09-09'</span>)*<span class="number">1000</span>);</span><br><span class="line"></span><br><span class="line">$snowflake-&gt;id();</span><br><span class="line">use Godruoyi\Snowflake\Snowflake;</span><br><span class="line">use Godruoyi\Snowflake\LaravelSequenceResolver;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">AppServiceProvider</span> <span class="keyword">extends</span> <span class="title">ServiceProvider</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Register any application services.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @return void</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">register</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;app-&gt;singleton(<span class="string">'snowflake'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> (<span class="keyword">new</span> Snowflake())</span><br><span class="line">                -&gt;setStartTimeStamp(strtotime(<span class="string">'2019-08-08'</span>)*<span class="number">1000</span>)</span><br><span class="line">                -&gt;setSequenceResolver(<span class="keyword">new</span> LaravelSequenceResolver(</span><br><span class="line">                    $<span class="keyword">this</span>-&gt;app-&gt;get(<span class="string">'cache'</span>)-&gt;store()</span><br><span class="line">                ));</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="number">64</span> 位的二进制最大能生成 <span class="number">19</span> 位的 ID，所以在设计数据库长度的时候最好预留下长度</span><br><span class="line">  public <span class="function"><span class="keyword">function</span> <span class="title">getCurrentMicrotime</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">      </span>&#123;</span><br><span class="line">          <span class="keyword">return</span> floor(microtime(<span class="literal">true</span>) * <span class="number">1000</span>) | <span class="number">0</span>;<span class="comment">//浮点数转数字 </span></span><br><span class="line">      &#125;</span><br><span class="line"> bindec(<span class="string">'0111111111111111111111111111111111111111111111111111111111111111'</span>)</span><br><span class="line">=&gt; <span class="number">9223372036854775807</span></span><br><span class="line">&gt;&gt;&gt; strlen(<span class="number">9223372036854775807</span>)</span><br><span class="line">=&gt; <span class="number">19</span></span><br><span class="line">https:<span class="comment">//learnku.com/articles/32575</span></span><br></pre></td></tr></table></figure>
<h3 id="Nginx-与-PHP-的通信"><a href="#Nginx-与-PHP-的通信" class="headerlink" title="Nginx 与 PHP 的通信"></a>Nginx 与 PHP 的通信</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"> Nginx 并不是直接和 PHP 进行通信的，而是通过 PHP-FPM。Nginx 不仅仅是一个强大的 Web 服务器，也是一个强大的代理服务器，提供了很多请求协议的代理。比如 Http 协议还有 FastCgi 协议等。</span><br><span class="line"></span><br><span class="line">当请求进入到 Nginx 中，Nginx 提供了一个 FastCgi 模块 来把 Http 请求映射为对应的 Fastcgi 请求。该模块提供了 fastcgi_param 指定来完成映射关系。它的主要作用就是把 Nginx 中的变量翻译成 PHP 中能够理解的变量</span><br><span class="line"></span><br><span class="line">fastcgi_pass, 用来指定 FPM 进程监听的地址，Nginx 会把所有的 PHP 请求映射成 fastcgi 请求，然后发送到这个地址上。</span><br><span class="line"></span><br><span class="line">所有的 .php 结尾的请求都交给 fastcgi 模块处理，然后把处理后的请求发送给 PHP-FPM，然后 PHP-FPM 把请求交给 worker 进程，worker 进程加载 PHP 解析器运行 PHP 处理结果。 其中 fastcgi_pass unix:<span class="regexp">/var/</span>run/php/php7<span class="number">.1</span>-fpm.sock; 这一行用来指定 fpm 的地址。</span><br><span class="line">Nginx 和 PHP 的通信流程大概如下:</span><br><span class="line"></span><br><span class="line">客户端发送请求到 Nginx</span><br><span class="line">加载 nginx.conf 文件，把所有 .php 结尾的请求特殊处理</span><br><span class="line">加载 FastCGI 模块，完成请求参数的解析映射，生成 FastCGI 请求</span><br><span class="line">然后通过 fastcgi_pass 参数把 FastCGI 请求发送给 PHP-FPM 处理</span><br><span class="line">PHP-FPM 收到请求，分配给空闲 worker 子进程</span><br><span class="line">worker 子进程加载 PHP 解析器等 完成 PHP 执行获取结果</span><br><span class="line">PHP-FPM 是一种 master/worker 进程架构。首先会启动一个 master 主进程，主要功能用来完成 PHP 环境的初始化，事件监听，子进程状态管理等等。然后会启动若干 worker 子进程来处理 PHP 请求。</span><br><span class="line"></span><br><span class="line">master 进程的工作流程</span><br><span class="line"></span><br><span class="line"><span class="number">1.</span> 初始化 CGI，注册进程信号初始化全局变量。</span><br><span class="line"><span class="number">2.</span> 完成 PHP 环境初始化。加载 php.ini 解析配置文件，加载 PHP 模块记录函数符号表，加载 zend 扩展，设置禁用函数和类库设置，注册回收内存方法。</span><br><span class="line"><span class="number">3.</span> 完成 PHP-FPM 初始化。加载并解析 php-fpm.conf 文件，获取进程相关参数，初始化进程池以及事件模型等。</span><br><span class="line"><span class="number">4.</span> 处理子进程相关操作。fork 子进程，进行事件监听等。</span><br><span class="line"></span><br><span class="line">worker 进程工作流程</span><br><span class="line"></span><br><span class="line"><span class="number">1.</span> 接收请求。这里是不需要初始化 PHP 运行环境的。</span><br><span class="line"><span class="number">2.</span> 处理请求。获取请求内容注册全局变量 ($_GET,$_POST,$_SERVER 等)，然后根据请求信息访问对应的 PHP 文件，然后将 PHP 脚本文件交给 Zend 引擎处理。</span><br><span class="line"><span class="number">3.</span> 处理结果。在 Zend 引擎处理完毕后将会进行回调，发送响应内容，释放内存等</span><br><span class="line"></span><br><span class="line">测试 php-fpm 配置内容是否正确 使用 -t 参数， 还可以通过加 -c 指定 php.ini 文件，通过 -y 指定 php-fpm.conf 文件 </span><br><span class="line"></span><br><span class="line">/usr/sbin/php-fpm7 -t</span><br><span class="line">/usr/sbin/php-fpm7 -c /usr/local/php/etc/php.ini -y /usr/local/php/etc/php-fpm.conf -t</span><br><span class="line"></span><br><span class="line">使用 root 权限启动子进程 通过增加 -R 参数</span><br><span class="line"></span><br><span class="line">/usr/sbin/php-fpm7 -c xxx/xxx/xxx/php.ini -y /xxx/xxx/xxx/php-fpm.conf -R</span><br><span class="line"></span><br><span class="line">重新创建容器 并添加 --cap-add=SYS_PTRACE 给容器追加 Ptrace 功能</span><br><span class="line"></span><br><span class="line">docker run --name website_name -p <span class="number">11280</span>:<span class="number">80</span> --cap-add=SYS_PTRACE -v /data/website/website_name:<span class="regexp">/app -d showtime/</span>php-javabridge:v1  https:<span class="comment">//learnku.com/articles/28683</span></span><br></pre></td></tr></table></figure>
<h3 id="Elasticsearch-Algolia-全文搜索"><a href="#Elasticsearch-Algolia-全文搜索" class="headerlink" title="Elasticsearch/Algolia 全文搜索"></a>Elasticsearch/Algolia 全文搜索</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//www.elastic.co/cn/downloads/elasticsearch</span></span><br><span class="line">新版的 ES 是内置的 java 环境</span><br><span class="line">windows 直接启动 elasticsearch.bat </span><br><span class="line"></span><br><span class="line">https:<span class="comment">//learnku.com/articles/30812#reply117421</span></span><br></pre></td></tr></table></figure>
<h3 id="PHP-系统调用time"><a href="#PHP-系统调用time" class="headerlink" title="PHP 系统调用time()"></a>PHP 系统调用time()</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">$year = date(<span class="string">'Y'</span>, time());</span><br><span class="line">$month = date(<span class="string">'m'</span>, time());</span><br><span class="line">系统调用就是操作系统提供给用户程序访问计算机资源的接口，更要命的是，系统调用极其耗时</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">TimeWrapper</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    private <span class="keyword">static</span> $now_time = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * @param bool $force_refresh 是否强制刷新</span></span><br><span class="line"><span class="comment">     * @return int</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">getTime</span>(<span class="params">$force_refresh = false</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> ($force_refresh) &#123;</span><br><span class="line">            self::$now_time = time();</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (!self::$now_time) &#123;</span><br><span class="line">                self::$now_time = time();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> self::$now_time;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/36538</span></span><br></pre></td></tr></table></figure>
<h3 id="curl-下载进度"><a href="#curl-下载进度" class="headerlink" title="curl 下载进度"></a>curl 下载进度</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Downloader</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">static</span> $lastDownloaded = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">static</span> $lastTime = <span class="literal">null</span>;</span><br><span class="line"></span><br><span class="line">	public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">download</span>(<span class="params">$url, $fileName, $date</span>)</span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		ini_set(<span class="string">'memory_limit'</span>, <span class="attr">Config</span>::$memory_limit);	<span class="comment">//调整最大占用内存</span></span><br><span class="line">		$code = [<span class="string">'"'</span>, <span class="string">'*'</span>, <span class="string">':'</span>, <span class="string">'&lt;'</span>, <span class="string">'&gt;'</span>, <span class="string">'？'</span>, <span class="string">'/'</span>, <span class="string">'\\'</span>, <span class="string">'|'</span>];</span><br><span class="line">		$fileName = preg_replace(<span class="string">'# #'</span>,<span class="string">''</span>,$fileName);</span><br><span class="line">		$fileName = str_replace($code, <span class="string">''</span>, $fileName);</span><br><span class="line">		<span class="keyword">if</span> (!is_dir(Config::$path)) &#123;</span><br><span class="line">			mkdir(Config::$path);</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		$filePath = Config::$path.<span class="string">'/'</span>.date(<span class="string">'Ymd'</span>,strtotime($date)).<span class="string">'_'</span>.$fileName.<span class="string">'.mp4'</span>;</span><br><span class="line">		<span class="keyword">if</span> (file_exists($filePath))&#123;</span><br><span class="line">			echo <span class="string">"\033[0;32m"</span>.<span class="string">"文件已存在"</span>.<span class="string">"\033[0m\n"</span>;</span><br><span class="line">			<span class="keyword">return</span>;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		$header = array();</span><br><span class="line">		$header[] = <span class="string">"User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36Name'"</span>;</span><br><span class="line">		$header[] = <span class="string">"Referer:http://91porn.com"</span>;</span><br><span class="line"></span><br><span class="line">		$ch = curl_init();</span><br><span class="line">		<span class="comment">// 从配置文件中获取根路径</span></span><br><span class="line">		curl_setopt($ch, CURLOPT_URL, $url);</span><br><span class="line">		curl_setopt($ch, CURLOPT_TIMEOUT,<span class="number">300</span>);</span><br><span class="line">		curl_setopt($ch, CURLOPT_RETURNTRANSFER, <span class="literal">true</span>);</span><br><span class="line">		curl_setopt($ch, CURLOPT_HTTPHEADER, $header);</span><br><span class="line">		<span class="keyword">if</span> (property_exists(<span class="string">'Config'</span>, <span class="string">'proxy'</span>)) &#123;</span><br><span class="line">			curl_setopt($ch, CURLOPT_PROXY, <span class="attr">Config</span>::$proxy);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="comment">// 开启进度条</span></span><br><span class="line">		curl_setopt($ch, CURLOPT_NOPROGRESS, <span class="literal">false</span>);</span><br><span class="line">		<span class="comment">// 进度条的触发函数</span></span><br><span class="line">		curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, array(<span class="keyword">new</span> self, <span class="string">'progress'</span>));</span><br><span class="line">		<span class="comment">// ps: 如果目标网页跳转，也跟着跳转</span></span><br><span class="line">		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, <span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line">		$data = curl_exec($ch);</span><br><span class="line">		curl_close($ch);</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">if</span> ($data) &#123;</span><br><span class="line">			$file = fopen($filePath,<span class="string">"w+"</span>);</span><br><span class="line">			fputs($file,$data);<span class="comment">//写入文件</span></span><br><span class="line">			fclose($file);</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="comment">// 使用rclone上传onedrive，其中“91porn:/91porn”对应网盘名称和路径</span></span><br><span class="line">		<span class="comment">// $command = 'rclone move -P '.$filePath.' 91porn:/91porn';</span></span><br><span class="line">		<span class="comment">// system($command);</span></span><br><span class="line"></span><br><span class="line">		unset($data);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	* 进度条下载.https://github.com/zzjzz9266a/91porn_php</span></span><br><span class="line"><span class="comment">	*</span></span><br><span class="line"><span class="comment">	* @param $ch</span></span><br><span class="line"><span class="comment">	* @param $downloadSize 总下载量</span></span><br><span class="line"><span class="comment">	* @param $downloaded 当前下载量</span></span><br><span class="line"><span class="comment">	* @param $uploadSize </span></span><br><span class="line"><span class="comment">	* @param $uploaded</span></span><br><span class="line"><span class="comment">	*/</span></span><br><span class="line">	<span class="function"><span class="keyword">function</span> <span class="title">progress</span>(<span class="params">$resource, $downloadSize = <span class="number">0</span>, $downloaded = <span class="number">0</span>, $uploadSize = <span class="number">0</span>, $uploaded = <span class="number">0</span></span>)</span>&#123;</span><br><span class="line">		<span class="keyword">if</span> ($downloadSize === <span class="number">0</span>) &#123;</span><br><span class="line">			<span class="keyword">return</span>;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">if</span> ($downloaded == $downloadSize) &#123;</span><br><span class="line">			printf(<span class="string">"下载完成: %.1f%%, %.2f MB/%.2f MB\n"</span>, $downloaded/$downloadSize*<span class="number">100</span>, $downloaded/<span class="number">1000000</span>, $downloadSize/<span class="number">1000000</span>);</span><br><span class="line">			Downloader::$lastDownloaded = <span class="number">0</span>;</span><br><span class="line">			Downloader::$lastTime = <span class="number">0</span>;</span><br><span class="line">			<span class="keyword">return</span>;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">if</span> (microtime(<span class="literal">true</span>)-Downloader::$lastTime &lt;= <span class="number">1</span>) &#123;</span><br><span class="line">			<span class="keyword">return</span>;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		$speed = ($downloaded-Downloader::$lastDownloaded)/(microtime(<span class="literal">true</span>)-Downloader::$lastTime)/<span class="number">1000</span>;</span><br><span class="line"></span><br><span class="line">		Downloader::$lastDownloaded = $downloaded;</span><br><span class="line">		Downloader::$lastTime = microtime(<span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line">		$downloaded = $downloaded/<span class="number">1000000</span>;</span><br><span class="line">		$downloadSize = $downloadSize/<span class="number">1000000</span>;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">if</span> ($speed &lt; <span class="number">1000</span>) &#123;</span><br><span class="line">			$speedStr = <span class="string">", 下载速度：%.2f kb/s     "</span>;</span><br><span class="line">		&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">			$speedStr = <span class="string">", 下载速度：%.2f mb/s     "</span>;</span><br><span class="line">			$speed = $speed/<span class="number">1000</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		$progress = $downloaded/$downloadSize*<span class="number">100</span>;</span><br><span class="line">		printf(<span class="string">"下载进度: %.1f%%, %.2f MB/%.2f MB"</span>.$speedStr.<span class="string">"\r"</span>, $progress, $downloaded, $downloadSize, $speed);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">random_ip</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	$a = rand(<span class="number">0</span>, <span class="number">255</span>);</span><br><span class="line">	$b = rand(<span class="number">0</span>, <span class="number">255</span>);</span><br><span class="line">	$c = rand(<span class="number">0</span>, <span class="number">255</span>);</span><br><span class="line">	$d = rand(<span class="number">0</span>, <span class="number">255</span>);</span><br><span class="line">	<span class="keyword">return</span> $a.<span class="string">'.'</span>.$b.<span class="string">'.'</span>.$c.<span class="string">'.'</span>.$d;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="队列执行频率限制"><a href="#队列执行频率限制" class="headerlink" title="队列执行频率限制"></a>队列执行频率限制</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">每分钟限制执行 <span class="number">10</span> 次 JOB（注意是 JOB，而不是整个队列）</span><br><span class="line"></span><br><span class="line">use Illuminate\Support\Facades\Redis;</span><br><span class="line">Redis::throttle(<span class="string">'key'</span>)-&gt;allow(<span class="number">10</span>)-&gt;every(<span class="number">60</span>)-&gt;then(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 任务逻辑...</span></span><br><span class="line">&#125;, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 无法获得锁...</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;release(<span class="number">10</span>);</span><br><span class="line">&#125;);</span><br><span class="line">并发，限制同一时间只执行一个 JOB</span><br><span class="line"></span><br><span class="line">Redis::funnel(<span class="string">'key'</span>)-&gt;limit(<span class="number">1</span>)-&gt;then(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 任务逻辑...</span></span><br><span class="line">&#125;, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 无法获得锁...</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;release(<span class="number">10</span>);</span><br><span class="line">&#125;);方法里的参数 key 是自定义的 redis key，如果需要多个 job 共用一个限制，则可以使用同一个 key</span><br><span class="line">   </span><br><span class="line">https:<span class="comment">//learnku.com/articles/30054</span></span><br></pre></td></tr></table></figure>
<h3 id="上传文件和下载"><a href="#上传文件和下载" class="headerlink" title="上传文件和下载"></a>上传文件和下载</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br></pre></td><td class="code"><pre><span class="line"> <span class="comment">/*</span></span><br><span class="line"><span class="comment">        文件上传函数</span></span><br><span class="line"><span class="comment">        @param  string  $name  文件上传文件域的name值</span></span><br><span class="line"><span class="comment">        @param  string  $dir   文件保存路径</span></span><br><span class="line"><span class="comment">        @param  array   $allow 文件允许上传的类型</span></span><br><span class="line"><span class="comment">        return  string  $filename 文件名  如果失败 返回false</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">upload</span>(<span class="params">$name,$dir=<span class="string">'./upload/'</span>,$allow=array(<span class="string">'jpg'</span>,<span class="string">'gif'</span>,<span class="string">'jpeg'</span>,<span class="string">'png'</span></span>))</span>&#123;</span><br><span class="line">        <span class="comment">//echo $name;exit;</span></span><br><span class="line">        <span class="comment">//var_dump($_FILES);exit;</span></span><br><span class="line">        <span class="comment">//1.判断文件上传错误</span></span><br><span class="line">        <span class="keyword">if</span>($_FILES[$name][<span class="string">'error'</span>]&gt;<span class="number">0</span>)&#123;</span><br><span class="line">            <span class="comment">//echo '上传错误';</span></span><br><span class="line">            <span class="keyword">switch</span>($_FILES[$name][<span class="string">'error'</span>])&#123;</span><br><span class="line">                <span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">                    echo <span class="string">'上传的文件超过了 php.ini 中upload_max_filesize 选项限制的值.'</span>;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="number">2</span>:</span><br><span class="line">                    echo <span class="string">'上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值'</span>;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="number">3</span>:</span><br><span class="line">                    echo <span class="string">'文件只有部分被上传.'</span>;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="number">4</span>:</span><br><span class="line">                    echo <span class="string">'没有文件被上传.'</span>;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="number">6</span>:</span><br><span class="line">                    echo <span class="string">'找不到临时文件夹.'</span>;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="number">7</span>:</span><br><span class="line">                    echo <span class="string">'文件写入失败.'</span>;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//2.判断你文件上传的类型是否是你想要的类型</span></span><br><span class="line">        <span class="comment">//2.1允许上传的类型</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//2.2 获取后缀名</span></span><br><span class="line">        $suffix = pathinfo($_FILES[$name][<span class="string">'name'</span>],PATHINFO_EXTENSION);</span><br><span class="line">        <span class="comment">//echo $suffix;exit;</span></span><br><span class="line">        <span class="comment">//2.3 判断是否是我们允许上传的类型</span></span><br><span class="line">        <span class="comment">//var_dump(in_array($suffix,$allow));exit;</span></span><br><span class="line">        <span class="keyword">if</span>(!in_array($suffix,$allow))&#123;</span><br><span class="line">            <span class="comment">//不允许上传的类型</span></span><br><span class="line">            echo  <span class="string">'大哥你的上传类型不符合'</span>;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//3.起名字</span></span><br><span class="line">        $filename = date(<span class="string">'Ymd'</span>).uniqid().mt_rand(<span class="number">0</span>,<span class="number">9999</span>).<span class="string">'.'</span>.$suffix;</span><br><span class="line">        <span class="comment">//echo $filename;exit;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//4.判断保存路径是否存在</span></span><br><span class="line">        <span class="comment">//4.1 得到保存路径</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//4.2 处理保存路径和后面的斜杠</span></span><br><span class="line">        $save_path = rtrim($dir,<span class="string">'/'</span>);</span><br><span class="line">        $save_path .=<span class="string">'/'</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//4.3 保存路径中的时间文件夹处理</span></span><br><span class="line">        $save_path .=date(<span class="string">'Y/m/d/'</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//4.4 判断保存的路径是否存在</span></span><br><span class="line">        <span class="keyword">if</span>(!file_exists($save_path))&#123;</span><br><span class="line">            mkdir($save_path,<span class="number">777</span>,<span class="literal">true</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//4.5 拼接一个完整的保存路径</span></span><br><span class="line">        $path = $save_path.$filename;</span><br><span class="line">        <span class="comment">//echo $path;exit;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//5.判断是否是httppost方式上传</span></span><br><span class="line">        <span class="keyword">if</span>(!is_uploaded_file($_FILES[$name][<span class="string">'tmp_name'</span>]))&#123;</span><br><span class="line">            echo <span class="string">'滚蛋！'</span>;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//6.移动图片</span></span><br><span class="line">        <span class="keyword">if</span>(!move_uploaded_file($_FILES[$name][<span class="string">'tmp_name'</span>],$path))&#123;</span><br><span class="line">            echo <span class="string">'移动失败'</span>;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//7.返回移动成功的图片名</span></span><br><span class="line">        <span class="keyword">return</span> $filename;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    下载文件</span><br><span class="line">     &lt;!-- 浏览器认识这样的类型,就会被解析 --&gt;</span><br><span class="line">        &lt;a href=<span class="string">"./action.php?name=1.html"</span>&gt;<span class="number">1.</span>html&lt;<span class="regexp">/a&gt;</span></span><br><span class="line"><span class="regexp">        &lt;a href="./</span>action.php?name=<span class="number">1.</span>php<span class="string">"&gt;1.php&lt;/a&gt;</span></span><br><span class="line"><span class="string">        &lt;a href="</span>./action.php?name=<span class="number">1.</span>txt<span class="string">"&gt;1.txt&lt;/a&gt;</span></span><br><span class="line"><span class="string">        &lt;a href="</span>./action.php?name=<span class="number">1.</span>jpg<span class="string">"&gt;1.jpg&lt;/a&gt;</span></span><br><span class="line"><span class="string">    //接收一下name值.</span></span><br><span class="line"><span class="string">    $name = $_GET['name'];</span></span><br><span class="line"><span class="string">    </span></span><br><span class="line"><span class="string">    //实现下载功能</span></span><br><span class="line"><span class="string">    //强制浏览器弹出另存为对话框</span></span><br><span class="line"><span class="string">    header('content-Disposition:attachment;filename="</span><span class="string">'.$name.'</span><span class="string">"');</span></span><br><span class="line"><span class="string">    </span></span><br><span class="line"><span class="string">    //此时只是下载了一个空文件,需要利用readfile读一遍所有的内容.便可下载.</span></span><br><span class="line"><span class="string">    $path = './downlist/'.$name;</span></span><br><span class="line"><span class="string">    readfile($path);</span></span><br><span class="line"><span class="string">https://learnku.com/articles/36744</span></span><br></pre></td></tr></table></figure>
<h3 id="app-接口对接"><a href="#app-接口对接" class="headerlink" title="app 接口对接"></a>app 接口对接</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (empty($arr)) &#123;</span><br><span class="line">   $arr = (object) <span class="literal">null</span>;<span class="comment">//$arr = new stdClass;</span></span><br><span class="line">&#125;</span><br><span class="line">echo json_encode($arr);</span><br><span class="line">$arr = [<span class="string">"id"</span>=&gt;<span class="string">"1"</span>,<span class="string">"name"</span>=&gt;<span class="literal">null</span>];</span><br><span class="line">$data = [</span><br><span class="line"> <span class="string">"id"</span>=&gt;(int) $arr[<span class="string">"id"</span>],</span><br><span class="line"><span class="string">"name"</span>=&gt;(string) $arr[<span class="string">"name"</span>],</span><br><span class="line">];</span><br><span class="line">echo json_encode($data);</span><br><span class="line">json_encode ($result ,JSON_FORCE_OBJECT)，返回格式为 &#123;&#125;,</span><br></pre></td></tr></table></figure>
<h3 id="异常类"><a href="#异常类" class="headerlink" title="异常类"></a>异常类</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">doSomething</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(something error)&#123;</span><br><span class="line">         <span class="keyword">throw</span> <span class="keyword">new</span> SomethingException(<span class="string">'something error'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    ...</span><br><span class="line">    <span class="keyword">do</span> something</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//调用</span></span><br><span class="line"><span class="keyword">try</span>&#123;</span><br><span class="line">   doSomething();</span><br><span class="line">&#125;<span class="keyword">catch</span>(SomethingException $e)&#123;</span><br><span class="line">   echo <span class="string">"something goes error"</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 调用方无法从外部得知时哪里出了问题，因为只返回了 false</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">checkoutOrder</span>(<span class="params">$orderNumber</span>)</span>&#123;</span><br><span class="line">  $orderModel = OrderModel::query()-&gt;where(<span class="string">'order_number'</span>,$orderNumber)-&gt;first();</span><br><span class="line">  <span class="keyword">if</span>(!$orderModel)&#123;</span><br><span class="line">     <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  $payResut = PayService::pay($orderModel);</span><br><span class="line">  <span class="keyword">if</span>(!$payResut)&#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">checkoutOrder</span>(<span class="params">$orderNumber</span>)</span>&#123;</span><br><span class="line">  $orderModel = OrderModel::query()-&gt;where(<span class="string">'order_number'</span>,$orderNumber)-&gt;first();</span><br><span class="line">  <span class="keyword">if</span>(!$orderModel)&#123;</span><br><span class="line">     <span class="keyword">throw</span> <span class="keyword">new</span> OrderNotFoundException($orderNumber);</span><br><span class="line">  &#125;</span><br><span class="line">  $payResut = PayService::pay($orderModel);</span><br><span class="line">  <span class="keyword">if</span>(!$payResut)&#123;</span><br><span class="line">      <span class="keyword">throw</span> <span class="keyword">new</span> PaymentException(); <span class="comment">// 这个异常应该上面的 PayService 中抛出，为了更清晰就写在这</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 调用</span></span><br><span class="line"><span class="keyword">try</span>&#123;</span><br><span class="line">    checkoutOrder(<span class="string">'ORDER00001'</span>);</span><br><span class="line">&#125;<span class="keyword">catch</span>(OrderNotFoundException $e)&#123;</span><br><span class="line">    <span class="keyword">return</span> response(<span class="string">'订单不存在：'</span>.$e-&gt;getMessage(),<span class="number">404</span>);</span><br><span class="line">&#125;<span class="keyword">catch</span>(PaymentException $e)&#123;</span><br><span class="line">    <span class="keyword">return</span> response(<span class="string">'支付失败：'</span>.$e-&gt;getMessage(),<span class="number">500</span>);</span><br><span class="line">&#125;</span><br><span class="line">app/Exceptions/Handler.php 中根据异常名、紧急程度调用第三方通知工具（钉钉、邮件等）通知项目错误。</span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">report</span>(<span class="params">Exception $exception</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span> ($<span class="keyword">this</span>-&gt;shouldntReport($exception)) &#123;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 如果异常类中存在 report 方法，就使用自身的</span></span><br><span class="line">    <span class="keyword">if</span> (method_exists($exception, <span class="string">'report'</span>)) &#123;</span><br><span class="line">        <span class="keyword">return</span> $exception-&gt;report();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    $msg = <span class="string">"系统异常："</span> . $exception-&gt;getMessage();</span><br><span class="line">    $msg .= <span class="string">"\n文件："</span> . $exception-&gt;getFile();</span><br><span class="line">    $msg .= <span class="string">"\n行号："</span> . $exception-&gt;getLine();</span><br><span class="line">    $msg .= <span class="string">"\n参数:"</span> . json_encode([<span class="string">'form_params'</span> =&gt; request()-&gt;all()]);</span><br><span class="line"></span><br><span class="line">    DingService::sendWarning($msg);</span><br><span class="line">    parent::report($exception);</span><br><span class="line">&#125;</span><br><span class="line">在 render 方法根据异常名返回不同的客户端响应：</span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">render</span>(<span class="params">$request, Exception $exception</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> ($exception <span class="keyword">instanceof</span> OrderException) &#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;handleOrderException($exception, $request);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> ($exception <span class="keyword">instanceof</span> PaymentException) &#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;handlePaymentException($exception, $request);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> parent::render($request, $exception);</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/36657</span></span><br></pre></td></tr></table></figure>
<h3 id="where-or-查询"><a href="#where-or-查询" class="headerlink" title="where or 查询"></a>where or 查询</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br></pre></td><td class="code"><pre><span class="line"> SELECT * FROM user</span><br><span class="line">  WHERE </span><br><span class="line">    group_id = <span class="string">'group id'</span></span><br><span class="line">   AND (</span><br><span class="line">     name = <span class="string">'name'</span></span><br><span class="line">     OR mobile_number = <span class="string">'mobile number'</span></span><br><span class="line">     OR email = <span class="string">'email'</span></span><br><span class="line">     OR <span class="string">`score`</span> &gt; <span class="number">1000</span></span><br><span class="line">   ) </span><br><span class="line">  DB::table(<span class="string">'users'</span>)</span><br><span class="line">     -&gt;where(<span class="string">'group_id'</span>, <span class="string">'group id'</span>)</span><br><span class="line">     -&gt;where(<span class="function"><span class="keyword">function</span> (<span class="params">$query</span>) </span>&#123;</span><br><span class="line">         $query-&gt;where(<span class="string">'name'</span>, <span class="string">'name'</span>)</span><br><span class="line">               -&gt;orWhere(<span class="string">'mobile_number'</span>, <span class="string">'='</span>, <span class="string">'mobile number'</span>)</span><br><span class="line">               -&gt;orWhere(<span class="string">'email'</span>, <span class="string">'='</span>, <span class="string">'email'</span>)</span><br><span class="line">               -&gt;orWhere(<span class="string">'score'</span>, <span class="string">'&gt;'</span>, <span class="string">'1000'</span>);</span><br><span class="line">     &#125;)</span><br><span class="line">     -&gt;get();</span><br><span class="line">DB::table(<span class="string">'users'</span>)</span><br><span class="line">     -&gt;where(<span class="string">'group_id'</span>, <span class="string">'group id'</span>)</span><br><span class="line">     -&gt;where(<span class="function"><span class="keyword">function</span> (<span class="params">$query</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> ($params[<span class="string">'name'</span>]) &#123;</span><br><span class="line">          $query-&gt;orWhere(<span class="string">'name'</span>, $params[<span class="string">'name'</span>])</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> ($params[<span class="string">'mobile_number'</span>]) &#123;</span><br><span class="line">          $query-&gt;orWhere(<span class="string">'mobile_number'</span>, $params[<span class="string">'mobile_number'</span>])</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> ($params[<span class="string">'email'</span>]) &#123;</span><br><span class="line">          $query-&gt;orWhere(<span class="string">'email'</span>, $params[<span class="string">'email'</span>])</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> ($params[<span class="string">'score'</span>]) &#123;</span><br><span class="line">          $query-&gt;orWhere(<span class="string">'score'</span>, <span class="string">'&gt;'</span>, $params[<span class="string">'score'</span>])</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">     &#125;)</span><br><span class="line">     -&gt;get();</span><br><span class="line">  $orWhere = [];</span><br><span class="line">  <span class="keyword">if</span> ($params[<span class="string">'name'</span>]) &#123;</span><br><span class="line">      $orWhere[] = [<span class="string">'name'</span>, <span class="string">'='</span>, $params[<span class="string">'name'</span>], <span class="string">'OR'</span>];</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">if</span> ($params[<span class="string">'mobile_number'</span>]) &#123;</span><br><span class="line">      $orWhere[] = [<span class="string">'mobile_number'</span>, <span class="string">'='</span>, $params[<span class="string">'mobile_number'</span>], <span class="string">'OR'</span>];</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">if</span> ($params[<span class="string">'email'</span>]) &#123;</span><br><span class="line">      $orWhere[] = [<span class="string">'email'</span>, <span class="string">'='</span>, $params[<span class="string">'email'</span>], <span class="string">'OR'</span>];</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">if</span> ($params[<span class="string">'score'</span>]) &#123;</span><br><span class="line">      $orWhere[] = [<span class="string">'score'</span>, <span class="string">'&gt;'</span>, $params[<span class="string">'score'</span>], <span class="string">'OR'</span>];</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  DB::table(<span class="string">'users'</span>)</span><br><span class="line">     -&gt;where(<span class="string">'group_id'</span>, <span class="string">'group id'</span>)</span><br><span class="line">     -&gt;where($orWhere)</span><br><span class="line">     -&gt;get();</span><br><span class="line">   $orWhere = [];</span><br><span class="line">    <span class="keyword">if</span> ($params[<span class="string">'name'</span>]) &#123;</span><br><span class="line">        $orWhere[<span class="string">'name'</span>] = $params[<span class="string">'name'</span>];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> ($params[<span class="string">'mobile_number'</span>]) &#123;</span><br><span class="line">       $orWhere[<span class="string">'mobile_number'</span>] = $params[<span class="string">'mobile_number'</span>];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> ($params[<span class="string">'email'</span>]) &#123;</span><br><span class="line">        $orWhere[<span class="string">'email'</span>] = $params[<span class="string">'email'</span>];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> ($params[<span class="string">'score'</span>]) &#123;</span><br><span class="line">        $orWhere[] = [<span class="string">'score'</span>, <span class="string">'&gt;'</span>, <span class="number">1000</span>, <span class="string">'OR'</span>];</span><br><span class="line">    &#125;</span><br><span class="line">  DB::table(<span class="string">'users'</span>)</span><br><span class="line">     -&gt;where(<span class="string">'group_id'</span>, <span class="string">'group id'</span>)</span><br><span class="line">     -&gt;where(<span class="function"><span class="keyword">function</span> (<span class="params">$query</span>) <span class="title">use</span> (<span class="params">$orWhere</span>) </span>&#123;</span><br><span class="line">        $query-&gt;orWhere($orWhere);</span><br><span class="line">     &#125;)</span><br><span class="line">     -&gt;get();</span><br><span class="line">  https:<span class="comment">//learnku.com/articles/36743</span></span><br></pre></td></tr></table></figure>
<h3 id="查询封装"><a href="#查询封装" class="headerlink" title="查询封装"></a>查询封装</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line">select * <span class="keyword">from</span> posts where title=<span class="string">'xxx'</span> and content=<span class="string">'xxxx'</span>;</span><br><span class="line"></span><br><span class="line">use Illuminate\Http\Request;</span><br><span class="line">use Illuminate\Database\Query\Builder;</span><br><span class="line"></span><br><span class="line">abstract <span class="class"><span class="keyword">class</span> <span class="title">QueryFilter</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line"></span><br><span class="line">    protected $request;</span><br><span class="line">    protected $builder;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">Request $request</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;request = $request;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">apply</span>(<span class="params">Builder $builder</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;builder = $builder;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;init();</span><br><span class="line"></span><br><span class="line">        foreach ($<span class="keyword">this</span>-&gt;filters() <span class="keyword">as</span> $name =&gt; $value) &#123;</span><br><span class="line">            <span class="keyword">if</span>($value)&#123;</span><br><span class="line">                <span class="keyword">if</span> (method_exists($<span class="keyword">this</span>, $name)) &#123;</span><br><span class="line">                    call_user_func_array([$<span class="keyword">this</span>, $name],[$value]);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;builder;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    protected <span class="function"><span class="keyword">function</span> <span class="title">init</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">//子类可以做一些初始化的查询</span></span><br><span class="line">    &#125;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">filters</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;request-&gt;all();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">namespace App\Services\Search\Db\Filter;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">PostFilter</span> <span class="keyword">extends</span> <span class="title">QueryFilter</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">title</span>(<span class="params">$title</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;builder-&gt;where(<span class="string">'title'</span>, <span class="string">'like'</span>, <span class="string">"%&#123;$title&#125;%"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">content</span>(<span class="params">$content</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;builder-&gt;where(<span class="string">'content'</span>,<span class="string">'like'</span>, <span class="string">"%&#123;$content&#125;%"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">use DB;</span><br><span class="line"></span><br><span class="line">DB::table(<span class="string">'posts'</span>)-&gt;where(<span class="function"><span class="keyword">function</span>(<span class="params">$query</span>)</span>&#123;</span><br><span class="line">    app(\App\Services\Search\Db\Filter\PostFilter::<span class="class"><span class="keyword">class</span>)-&gt;<span class="title">apply</span>($<span class="title">query</span>)</span>;</span><br><span class="line"></span><br><span class="line">&#125;)-&gt;get();</span><br><span class="line">https:<span class="comment">//learnku.com/articles/36716</span></span><br></pre></td></tr></table></figure>
<h3 id="redis同步到数据库"><a href="#redis同步到数据库" class="headerlink" title="redis同步到数据库"></a>redis同步到数据库</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br></pre></td><td class="code"><pre><span class="line">namespace App\Models\Traits;</span><br><span class="line"></span><br><span class="line">use Redis;</span><br><span class="line">use Carbon\Carbon;</span><br><span class="line"></span><br><span class="line">trait ViewCountsHelper &#123;</span><br><span class="line">    <span class="comment">// 缓存相关</span></span><br><span class="line">    protected $hash_prefix = <span class="string">'topic_view_counts_'</span>;</span><br><span class="line">    protected $field_prefix = <span class="string">'topic_'</span>;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">viewCountIncrement</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// 获取今日 Redis 哈希表名称，如：topic_view_counts_2017-10-21</span></span><br><span class="line">        $hash = $<span class="keyword">this</span>-&gt;getHashFromDateString(Carbon::now()-&gt;toDateString());</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 字段名称，如：topic_1</span></span><br><span class="line">        $field = $<span class="keyword">this</span>-&gt;getHashField();</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 当前阅读数，如果存在就自增，否则就为 1</span></span><br><span class="line">        $count = Redis::hGet($hash, $field);</span><br><span class="line">        <span class="keyword">if</span> ($count) &#123;</span><br><span class="line">            $count++;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            $count = <span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 数据写入 Redis ，字段已存在会被更新</span></span><br><span class="line">        Redis::hSet($hash, $field, $count);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">syncTopicViewCounts</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// 获取昨日的哈希表名称，如：topic_view_counts_2017-10-21</span></span><br><span class="line">        $hash = $<span class="keyword">this</span>-&gt;getHashFromDateString(Carbon::now()-&gt;toDateString());</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 从 Redis 中获取所有哈希表里的数据</span></span><br><span class="line">        $counts = Redis::hGetAll($hash);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 如果没有任何数据直接 return</span></span><br><span class="line">        <span class="keyword">if</span> (count($counts) === <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 遍历，并同步到数据库中</span></span><br><span class="line">        foreach ($counts <span class="keyword">as</span> $topic_id =&gt; $view_count) &#123;</span><br><span class="line">            <span class="comment">// 会将 `topic_1` 转换为 1</span></span><br><span class="line">            $topic_id = str_replace($<span class="keyword">this</span>-&gt;field_prefix, <span class="string">''</span>, $topic_id);</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 只有当话题存在时才更新到数据库中</span></span><br><span class="line">            <span class="keyword">if</span> ($topic = $<span class="keyword">this</span>-&gt;find($topic_id)) &#123;</span><br><span class="line">                $topic-&gt;view_count = $<span class="keyword">this</span>-&gt;attribute[<span class="string">'view_count'</span>] + $view_count;</span><br><span class="line">                $topic-&gt;save();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 以数据库为中心的存储，既已同步，即可删除</span></span><br><span class="line">        Redis::del($hash);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">getViewCountAttribute</span>(<span class="params">$value</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// 获取今日对应的哈希表名称</span></span><br><span class="line">        $hash = $<span class="keyword">this</span>-&gt;getHashFromDateString(Carbon::now()-&gt;toDateString());</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 字段名称，如：topic_1</span></span><br><span class="line">        $field = $<span class="keyword">this</span>-&gt;getHashField();</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 三元运算符，优先选择 Redis 的数据，否则使用数据库中</span></span><br><span class="line">        $count = Redis::hGet($hash, $field) ? : $value;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 如果存在的话，返回 数据库中的阅读数 加上 Redis 中的阅读数</span></span><br><span class="line">        <span class="keyword">if</span> ($count) &#123;</span><br><span class="line">            <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;attribute[<span class="string">'view_count'</span>] + $count;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">// 否则返回 0</span></span><br><span class="line">            <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">getHashFromDateString</span>(<span class="params">$date</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// Redis 哈希表的命名，如：topic_view_counts_2017-10-21</span></span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;hash_prefix . $date;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">getHashField</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// 字段名称，如：topic_1</span></span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;field_prefix . $<span class="keyword">this</span>-&gt;id;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">在需要此功能的模型中 use Traits\ViewCountsHelper 即可</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">namespace App\Console\Commands;</span><br><span class="line"></span><br><span class="line">use Illuminate\Console\Command;</span><br><span class="line">use App\Models\Topic;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SyncTopicViewCounts</span> <span class="keyword">extends</span> <span class="title">Command</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    protected $signature = <span class="string">'topic:sync-topic-view-counts'</span>;</span><br><span class="line">    protected $description = <span class="string">'将话题 view_count 从 Redis 同步到数据库中'</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Execute the console command.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @return mixed</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">handle</span>(<span class="params">Topic $topic</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $topic-&gt;syncTopicViewCounts();</span><br><span class="line">        $<span class="keyword">this</span>-&gt;info(<span class="string">"同步成功！"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">namespace App\Http\Controllers;</span><br><span class="line"></span><br><span class="line">use App\Models\Topic;</span><br><span class="line">use Illuminate\Http\Request;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">TopicsController</span> <span class="keyword">extends</span> <span class="title">Controller</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">show</span>(<span class="params">Topic $topic</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $topic-&gt;viewCountIncrement(); <span class="comment">// 自增浏览数</span></span><br><span class="line"></span><br><span class="line">        dd($topic-&gt;view_count); <span class="comment">// 获取浏览数</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/32615</span></span><br></pre></td></tr></table></figure>
<h3 id="where-in"><a href="#where-in" class="headerlink" title="where in"></a>where in</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">$where[<span class="string">'status'</span>] = <span class="number">1</span>;</span><br><span class="line">$ids = [<span class="number">1</span>,<span class="number">2</span>];</span><br><span class="line">$where[] = [<span class="function"><span class="keyword">function</span>(<span class="params">$query</span>) <span class="title">use</span> (<span class="params">$ids</span>)</span>&#123;</span><br><span class="line">    $query-&gt;whereIn(<span class="string">'id'</span>, $ids);</span><br><span class="line">&#125;];</span><br><span class="line">$list = User::where($where)</span><br><span class="line">    -&gt;get();</span><br><span class="line">生成 sql 如下</span><br><span class="line"></span><br><span class="line">select * <span class="keyword">from</span> <span class="string">`users`</span> where (<span class="string">`status`</span> = <span class="number">1</span> and (<span class="string">`id`</span> <span class="keyword">in</span> (<span class="number">1</span>, <span class="number">2</span>)))</span><br><span class="line"></span><br><span class="line">$status = <span class="number">1</span>;</span><br><span class="line">$ids = [<span class="number">1</span>,<span class="number">2</span>];</span><br><span class="line">User::when($status, <span class="function"><span class="keyword">function</span> (<span class="params">$query, $status</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $query-&gt;where(<span class="string">'status'</span>, $status);</span><br><span class="line">    &#125;)</span><br><span class="line">        -&gt;when($ids, <span class="function"><span class="keyword">function</span> (<span class="params">$query, $ids</span>) </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> $query-&gt;whereIn(<span class="string">'id'</span>, $ids);</span><br><span class="line">        &#125;)</span><br><span class="line">        -&gt;get();</span><br><span class="line">https:<span class="comment">//learnku.com/articles/36964</span></span><br></pre></td></tr></table></figure>
<h3 id="付费文章试读"><a href="#付费文章试读" class="headerlink" title="付费文章试读"></a>付费文章试读</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">对 HTML 进行截断：</span><br><span class="line"></span><br><span class="line">closetags(str_limit($article-&gt;body, <span class="number">1000</span>))</span><br><span class="line">闭合标签：</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">closetags</span>(<span class="params">$html</span>) </span>&#123;</span><br><span class="line">    libxml_use_internal_errors(<span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line">    $dom = <span class="keyword">new</span> \DOMDocument;</span><br><span class="line">    $dom-&gt;loadHTML(<span class="string">'&lt;meta http-equiv="content-type" content="text/html; charset=utf-8"&gt;'</span> . $html);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Strip wrapping &lt;html&gt; and &lt;body&gt; tags</span></span><br><span class="line">    $mock = <span class="keyword">new</span> \DOMDocument;</span><br><span class="line">    $body = $dom-&gt;getElementsByTagName(<span class="string">'body'</span>)-&gt;item(<span class="number">0</span>);</span><br><span class="line">    foreach ($body-&gt;childNodes <span class="keyword">as</span> $child) &#123;</span><br><span class="line">        $mock-&gt;appendChild($mock-&gt;importNode($child, <span class="literal">true</span>));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> trim($mock-&gt;saveHTML());</span><br><span class="line">&#125;https:<span class="comment">//learnku.com/laravel/t/36859</span></span><br></pre></td></tr></table></figure>
<h3 id="全局记录管理员的所有操作"><a href="#全局记录管理员的所有操作" class="headerlink" title="全局记录管理员的所有操作"></a>全局记录管理员的所有操作</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line">namespace App\Providers;</span><br><span class="line">use App\Models\AdminLog;</span><br><span class="line">use Illuminate\Support\Facades\Auth;</span><br><span class="line">use Illuminate\Support\Facades\Event;</span><br><span class="line">use Illuminate\Support\ServiceProvider;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LogServiceProvider</span> <span class="keyword">extends</span> <span class="title">ServiceProvider</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Register services.</span></span><br><span class="line"><span class="comment">     *https://gist.github.com/Hanson/b8916bf13c38336f8eb33e537fab3723</span></span><br><span class="line"><span class="comment">     * @return void</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">boot</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        Event::listen(<span class="string">'eloquent.*'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">$eventName, $data</span>) </span>&#123;</span><br><span class="line">            <span class="keyword">if</span> (preg_match(<span class="string">'/eloquent\.(.+):\s(.+)/'</span>, $eventName, $match) === <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">/** $match @val array</span></span><br><span class="line"><span class="comment">            array (</span></span><br><span class="line"><span class="comment">                0 =&gt; 'eloquent.booting: App\\Models\\Groupon',</span></span><br><span class="line"><span class="comment">                1 =&gt; 'booting',</span></span><br><span class="line"><span class="comment">                2 =&gt; 'App\\Models\\Groupon',</span></span><br><span class="line"><span class="comment">            )</span></span><br><span class="line"><span class="comment">             */</span></span><br><span class="line">            <span class="comment">// only record when 'created', 'updated', 'deleted'</span></span><br><span class="line">            <span class="keyword">if</span> (!in_array($match[<span class="number">1</span>], [<span class="string">'created'</span>, <span class="string">'updated'</span>, <span class="string">'deleted'</span>])) &#123;</span><br><span class="line">                <span class="keyword">return</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// only record the admin operation.</span></span><br><span class="line">            <span class="keyword">if</span> (!Auth::guard(<span class="string">'admin'</span>)-&gt;check()) &#123;</span><br><span class="line">                <span class="keyword">return</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            $model = $data[<span class="number">0</span>];</span><br><span class="line">            $<span class="class"><span class="keyword">class</span> </span>= get_class($model);</span><br><span class="line">            $diff = array_diff_assoc($model-&gt;getOriginal(), $model-&gt;getAttributes());</span><br><span class="line">            $keys = array_keys($diff);</span><br><span class="line">            $data = [];</span><br><span class="line">            foreach ($keys <span class="keyword">as</span> $key) &#123;</span><br><span class="line">                <span class="keyword">if</span> ($key === <span class="string">'updated_at'</span>) &#123;</span><br><span class="line">                    <span class="keyword">continue</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                $data[$key] = [</span><br><span class="line">                    <span class="string">'old'</span> =&gt; $model-&gt;getOriginal($key),</span><br><span class="line">                    <span class="string">'new'</span> =&gt; $model-&gt;getAttributes()[$key]</span><br><span class="line">                ];</span><br><span class="line">            &#125;</span><br><span class="line">            $admin = Auth::guard(<span class="string">'admin'</span>)-&gt;user();</span><br><span class="line">            <span class="comment">// You can create the table with your situation</span></span><br><span class="line">            AdminLog::query()-&gt;create([</span><br><span class="line">                <span class="string">'admin_id'</span> =&gt; $admin-&gt;id,</span><br><span class="line">                <span class="string">'url'</span> =&gt; request()-&gt;fullUrl(),</span><br><span class="line">                <span class="string">'action'</span> =&gt; $match[<span class="number">1</span>], <span class="comment">// updated created deleted</span></span><br><span class="line">                <span class="string">'ip'</span> =&gt; request()-&gt;getClientIp(),</span><br><span class="line">                <span class="string">'model_id'</span> =&gt; $model-&gt;id,</span><br><span class="line">                <span class="string">'model_type'</span> =&gt; $<span class="class"><span class="keyword">class</span>,</span></span><br><span class="line"><span class="class">                '<span class="title">data</span>' </span>=&gt; $data,</span><br><span class="line">                <span class="string">'created_at'</span> =&gt; now(),</span><br><span class="line">            ]);</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h3 id="Column-not-found-1054-Unknown-column"><a href="#Column-not-found-1054-Unknown-column" class="headerlink" title="Column not found: 1054 Unknown column"></a>Column not found: 1054 Unknown column</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">$res = Model::where(<span class="string">'date'</span>,date(<span class="string">'Ymd'</span>))</span><br><span class="line">-&gt;whereraw(<span class="string">'(a=1 or b=2)'</span>)</span><br><span class="line"></span><br><span class="line">$date=date(<span class="string">'Ymd'</span>);</span><br><span class="line">$res = Model::where(<span class="string">'date'</span>,$date)</span><br><span class="line">-&gt;whereraw(<span class="string">'(a=1 or b=2)'</span>)</span><br><span class="line">提示Column not found: <span class="number">1054</span> Unknown column</span><br><span class="line">$res=Model::where(<span class="string">'date'</span>,$date)</span><br><span class="line">-&gt;where(<span class="function"><span class="keyword">function</span> (<span class="params">$query</span>) <span class="title">use</span>(<span class="params">$date</span>)</span>&#123;</span><br><span class="line">                        $query-&gt;where(<span class="string">'a'</span>, <span class="string">'&gt;'</span>, $date)-&gt;orwhere(<span class="string">'b'</span>, <span class="string">'2'</span>);</span><br><span class="line">                    &#125;)</span><br></pre></td></tr></table></figure>
<h3 id="groupBy-分组查询的分页"><a href="#groupBy-分组查询的分页" class="headerlink" title="groupBy 分组查询的分页"></a>groupBy 分组查询的分页</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">use Illuminate\Pagination\LengthAwarePaginator;</span><br><span class="line">use Illuminate\Support\Facades\DB;</span><br><span class="line">use App\Models\UserHasCard;</span><br><span class="line">...</span><br><span class="line">$page = <span class="number">1</span>;<span class="comment">//当前页</span></span><br><span class="line">$pageSize = <span class="number">10</span>;</span><br><span class="line">$model = UserHasCard::groupBy(<span class="string">'user_id'</span>);</span><br><span class="line">$count = DB::table($model)-&gt;count();</span><br><span class="line">$data = $model-&gt;limit($pageSize)-&gt;offset(($page<span class="number">-1</span>)*$pageSize)-&gt;get();</span><br><span class="line">$list = <span class="keyword">new</span> LengthAwarePaginator($data,$count,$pageSize,$page);</span><br><span class="line">https:<span class="comment">//learnku.com/articles/37239</span></span><br></pre></td></tr></table></figure>
<h3 id="php-artisan-tinker编码"><a href="#php-artisan-tinker编码" class="headerlink" title="php artisan tinker编码"></a>php artisan tinker编码</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">&gt;&gt;&gt; \App\Ip::where(<span class="string">'zt'</span>, <span class="string">'未付'</span>)-&gt; get();</span><br><span class="line">Illuminate\Database\QueryException <span class="keyword">with</span> message <span class="string">'SQLSTATE[HY000]: General error: 1267 Illegal mix of collations (gbk_chinese_ci,IMPLICIT) and (utf8_unicode_ci,COERCIBLE) for operation '</span>=<span class="string">' (S</span></span><br><span class="line"><span class="string">QL: select * from `ip` where `zt` = 未付)'</span></span><br><span class="line"></span><br><span class="line">\App\Ip::query(<span class="string">'select convert(zt USING gbk) from ip where zt = "未付"'</span>)-&gt;limit(<span class="number">2</span>)-&gt;get()</span><br><span class="line">&gt;&gt;&gt; \App\Ip::query(<span class="string">'select convert(zt USING gbk) where zt = 未付'</span>)-&gt;limit(<span class="number">2</span>)-&gt;get()</span><br><span class="line">select convert(zt USING gbk) <span class="keyword">from</span> <span class="string">`ip`</span> where zt = <span class="string">"未付"</span></span><br><span class="line">https:<span class="comment">//learnku.com/laravel/t/37299</span></span><br></pre></td></tr></table></figure>
<h3 id="api文档l5-swagger"><a href="#api文档l5-swagger" class="headerlink" title="api文档l5-swagger"></a>api文档l5-swagger</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">composer <span class="built_in">require</span> darkaonline/l5-swagger</span><br><span class="line">php artisan vendor:publish --provider <span class="string">"L5Swagger\L5SwaggerServiceProvider"</span></span><br><span class="line">http:<span class="comment">//122.51.155.172:8000/api/documentation</span></span><br><span class="line"></span><br><span class="line">vi app/Http/Controllers/MyController.php</span><br><span class="line">&lt;?php</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @OA\Info(title="Test", version="0.0.1")</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="comment">// 别直接复制这里的注释，缩进破坏了，</span></span><br><span class="line"><span class="comment">// 请上 github 上复制它的用例。这里只作为演示作用</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyController</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @OA\Get(</span></span><br><span class="line"><span class="comment"> *     path="/api/syahi",</span></span><br><span class="line"><span class="comment"> *     @OA\Response(response="200", description="An example resource")</span></span><br><span class="line"><span class="comment"> * )</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">say</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> [<span class="string">'msg'</span> =&gt; <span class="string">'Hello World'</span>];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// Other Code...</span></span><br><span class="line">&#125;</span><br><span class="line">php artisan l5-swagger:generate</span><br><span class="line"></span><br><span class="line">它会生成 API 文档，默认在项目根目录： storage\api-docs\api-docs.json</span><br><span class="line">https:<span class="comment">//learnku.com/articles/37313#reply119615</span></span><br></pre></td></tr></table></figure>
<h3 id="Laravel-里面的-chunk-分块效率"><a href="#Laravel-里面的-chunk-分块效率" class="headerlink" title="Laravel 里面的 chunk 分块效率"></a>Laravel 里面的 chunk 分块效率</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line"> public <span class="function"><span class="keyword">function</span> <span class="title">chunkTest</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// 每次处理</span></span><br><span class="line">        $speed = <span class="number">1000</span>;</span><br><span class="line">        <span class="comment">// 进度条</span></span><br><span class="line">        $bar = $<span class="keyword">this</span>-&gt;output-&gt;createProgressBar(TestModel::query()-&gt;count());</span><br><span class="line">        <span class="comment">// 记录开始时间</span></span><br><span class="line">        $timeStart = $<span class="keyword">this</span>-&gt;microtime_float();</span><br><span class="line">        <span class="comment">// chunk 分块处理数据</span></span><br><span class="line">        TestModel::query()-&gt;chunk($speed, <span class="function"><span class="keyword">function</span> (<span class="params">$item</span>) <span class="title">use</span> (<span class="params">$bar, $speed</span>) </span>&#123;</span><br><span class="line">        <span class="comment">// 业务处理逻辑...</span></span><br><span class="line">        <span class="comment">// ....</span></span><br><span class="line">        <span class="comment">// 进度条步进&#123;$speed&#125;步\  $bar-&gt;advance($speed);</span></span><br><span class="line">        &#125;);</span><br><span class="line">        $bar-&gt;finish();</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 处理完成，记录结束时间</span></span><br><span class="line">        $timeEnd = $<span class="keyword">this</span>-&gt;microtime_float();</span><br><span class="line">        $time = $timeEnd - $timeStart;</span><br><span class="line">        <span class="comment">// 输出信息</span></span><br><span class="line">        $<span class="keyword">this</span>-&gt;info(<span class="string">'chunk用时:'</span>. $time);</span><br><span class="line">    &#125;</span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">idTest</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 进度条</span></span><br><span class="line">    $bar = $<span class="keyword">this</span>-&gt;output-&gt;createProgressBar(TestModel::query()-&gt;count());</span><br><span class="line">    $timeStart = $<span class="keyword">this</span>-&gt;microtime_float();</span><br><span class="line">    <span class="comment">// 记录最大的id</span></span><br><span class="line">    $maxId = <span class="number">0</span>;</span><br><span class="line">    <span class="comment">// 每次处理多少条数据</span></span><br><span class="line">    $speed = <span class="number">1000</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> (<span class="literal">true</span>) &#123;</span><br><span class="line">        $models = TestModel::query()</span><br><span class="line">            <span class="comment">// 每次循环加上id条件</span></span><br><span class="line">            -&gt;where(<span class="string">'id'</span>, <span class="string">'&gt;'</span>, $maxId)</span><br><span class="line">            -&gt;limit($speed)</span><br><span class="line">            -&gt;orderBy(<span class="string">'id'</span>)</span><br><span class="line">            -&gt;get();</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 处理具体业务逻辑...</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 如果没有数据就代表处理完成，break；</span></span><br><span class="line">        <span class="keyword">if</span> ($models-&gt;isEmpty()) &#123;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 记录下本次的最大id，下次循环时当作条件</span></span><br><span class="line">        $maxId = $models-&gt;max([<span class="string">'id'</span>]);</span><br><span class="line"></span><br><span class="line">        $bar-&gt;advance($speed);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    $timeEnd = $<span class="keyword">this</span>-&gt;microtime_float();</span><br><span class="line">    $time = $timeEnd - $timeStart;</span><br><span class="line">    $bar-&gt;finish();</span><br><span class="line">    $<span class="keyword">this</span>-&gt;info(<span class="string">'id条件用时: '</span>. $time);</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/37541</span></span><br></pre></td></tr></table></figure>
<h3 id="Facade-demo"><a href="#Facade-demo" class="headerlink" title="Facade demo"></a>Facade demo</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line">php artisan make:provider MyServiceProvider</span><br><span class="line">namespace App\Providers;</span><br><span class="line">use B;</span><br><span class="line">use Illuminate\Support\ServiceProvider;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">TestProvider</span> <span class="keyword">extends</span> <span class="title">ServiceProvider</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Register services.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * [[@return](https://learnku.com/users/31554)](https://learnku.com/users/31554) void</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">register</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;app-&gt;singleton(<span class="string">'my-facade'</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">new</span> B();</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Bootstrap services.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * [[@return](https://learnku.com/users/31554)](https://learnku.com/users/31554) void</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">boot</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">//</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">B.php</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">B</span></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">b</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        echo <span class="string">'this class  B  function a ,you are right~!'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">在 app.php 中 分别加入</span><br><span class="line"><span class="string">'providers'</span>=&gt;[</span><br><span class="line">        ......</span><br><span class="line">        App\Providers\MyServiceProvider::<span class="class"><span class="keyword">class</span>,</span></span><br><span class="line">]</span><br><span class="line"><span class="string">'aliases'</span> =&gt; [</span><br><span class="line">        ......</span><br><span class="line">        <span class="string">'MyFacade'</span>=&gt;\App\Providers\Facades\MyProvider::<span class="class"><span class="keyword">class</span>,</span></span><br><span class="line">]</span><br><span class="line">在 app/Providers 文件夹下创建 Facades 文件夹 并创建 MyFacade.php 文件</span><br><span class="line">namespace App\Providers\Facades;</span><br><span class="line">use Illuminate\Support\Facades\Facade;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyFacade</span> <span class="keyword">extends</span> <span class="title">Facade</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">getFacadeAccessor</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">'my-facade'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">\MyFacade::b();</span><br><span class="line">https:<span class="comment">//learnku.com/articles/36610</span></span><br></pre></td></tr></table></figure>
<h3 id="验证码（Captcha）包乱码"><a href="#验证码（Captcha）包乱码" class="headerlink" title="验证码（Captcha）包乱码"></a>验证码（Captcha）包乱码</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//github.com/Gregwar/Captcha</span></span><br><span class="line">use Gregwar\Captcha\CaptchaBuilder;</span><br><span class="line">$code = <span class="keyword">new</span> CaptchaBuilder;</span><br><span class="line">$code-&gt;build(<span class="number">102</span>, <span class="number">35</span>);</span><br><span class="line">header(<span class="string">'Content-type: image/jpeg'</span>);</span><br><span class="line">$code-&gt;output();</span><br><span class="line"></span><br><span class="line">我是在laravel中使用，会出现乱码。但是调用CaptchaBuilder::save()正常生成图片。</span><br><span class="line">群里的朋友说，可以在最有加一个exit();我试了一下，能行。 应该是在laravel的控制器中执行完毕会有内容输出，影响到了验证码的正常输出。但是加exit()总不是办法，会影响到session</span><br><span class="line">然后换成ob_end_clean()放在最前面，也能行。 我有一篇博客说过，ob_end_clean()和ob_clean()然后换成ob_clean()放在最前面试了一下，不行。 所以猜测，laravel在控制器结束后，会往缓冲区写点东西。而ob_clean()只清空缓冲区，并没有关闭。 所以无法正常输出！http:<span class="comment">//www.shiguopeng.cn/archives/24</span></span><br></pre></td></tr></table></figure>
<h3 id="laravel权限设计"><a href="#laravel权限设计" class="headerlink" title="laravel权限设计"></a>laravel权限设计</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line">用户表 角色表 用户角色表 权限表 权限角色表</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">User</span> <span class="keyword">extends</span> <span class="title">Authenticatable</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    use Notifiable;</span><br><span class="line"> </span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * The attributes that should be hidden for arrays.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @var array</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    protected $hidden = [</span><br><span class="line">        <span class="string">'password'</span>, <span class="string">'remember_token'</span>,</span><br><span class="line">    ];</span><br><span class="line"> </span><br><span class="line">    <span class="comment">// 用户和角色的模型关联关系</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">roles</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;belongsToMany(Role::<span class="class"><span class="keyword">class</span>)</span>;</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="comment">/****************************************</span></span><br><span class="line"><span class="comment">    * 封装一个方法方便使用</span></span><br><span class="line"><span class="comment">    * 1. 需要的权限</span></span><br><span class="line"><span class="comment">    * 2. 遍历当期那用户拥有的所有角色</span></span><br><span class="line"><span class="comment">    * 3. 再通过角色判断是否有当前需要的权限</span></span><br><span class="line"><span class="comment">    ****************************************/</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">hasPermission</span>(<span class="params">$permissionName</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        foreach ($<span class="keyword">this</span>-&gt;roles <span class="keyword">as</span> $role) &#123;</span><br><span class="line">            <span class="keyword">if</span> ($role-&gt;permisssions()-&gt;where(<span class="string">'name'</span>, $permissionName)-&gt;exists()) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="literal">true</span>;;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"> </span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Role</span> <span class="keyword">extends</span> <span class="title">Model</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">// 用户和角色的模型关联关系</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">users</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;belongsToMany(User::<span class="class"><span class="keyword">class</span>)</span>;</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="comment">// 角色和权限的模型关联关系</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">permissions</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;belongsToMany(Permission::<span class="class"><span class="keyword">class</span>)</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Permission</span> <span class="keyword">extends</span> <span class="title">Model</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">// 角色和权限的模型关联关系</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">roles</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;belongsToMany(Role::<span class="class"><span class="keyword">class</span>)</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">http:<span class="comment">//www.shiguopeng.cn/archives/260 </span></span><br><span class="line">https:<span class="comment">//github.com/spatie/laravel-permission</span></span><br></pre></td></tr></table></figure>
<h3 id="路由多版本拆分"><a href="#路由多版本拆分" class="headerlink" title="路由多版本拆分"></a>路由多版本拆分</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 路由加载 多版本</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    protected <span class="function"><span class="keyword">function</span> <span class="title">mapAppRoutes</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="comment">// 获取路由文件夹下的所有目录和文件</span></span><br><span class="line">        foreach (scandir(base_path(<span class="string">'routes'</span>)) <span class="keyword">as</span> $dir)&#123;</span><br><span class="line"></span><br><span class="line">            <span class="comment">//匹配所有以v开头的目录作为版本号</span></span><br><span class="line">            <span class="keyword">if</span>(starts_with($dir,<span class="string">'v'</span>))&#123;</span><br><span class="line"></span><br><span class="line">                <span class="comment">//匹配目录下的php文件</span></span><br><span class="line">                foreach (glob(base_path(<span class="string">'routes/'</span>.$dir) . <span class="string">'/*.php'</span>) <span class="keyword">as</span> $file) &#123;</span><br><span class="line"></span><br><span class="line">                    <span class="comment">//前缀 目录版本号/文件名</span></span><br><span class="line">                    Route::prefix($dir.<span class="string">'/'</span>.basename($file,<span class="string">'.php'</span>))</span><br><span class="line">                        -&gt;middleware(<span class="string">'api'</span>)</span><br><span class="line">                        <span class="comment">//设置命名空间</span></span><br><span class="line">                        -&gt;namespace($<span class="keyword">this</span>-&gt;namespace.<span class="string">'\\'</span>.ucfirst(basename($file,<span class="string">'.php'</span>)))</span><br><span class="line">                        -&gt;group($file);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">路由目录文件如下https:<span class="comment">//learnku.com/articles/37684</span></span><br><span class="line"></span><br><span class="line">├── routes</span><br><span class="line">│   └── v1</span><br><span class="line">│       ├── a.php</span><br><span class="line">│       ├── b.php</span><br><span class="line">│   └── v2</span><br><span class="line">│       ├── c.php</span><br></pre></td></tr></table></figure>
<h3 id="lua-脚本-运行-Redis-命令"><a href="#lua-脚本-运行-Redis-命令" class="headerlink" title="lua 脚本 运行 Redis 命令"></a>lua 脚本 运行 Redis 命令</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">IncrementExistingKeysBy</span> <span class="keyword">extends</span> <span class="title">ScriptCommand</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">      public <span class="function"><span class="keyword">function</span> <span class="title">getKeysCount</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">      </span>&#123;  <span class="comment">// Tell Predis to use all the arguments but the last one as arguments</span></span><br><span class="line">       <span class="comment">// for KEYS. The last one will be used to populate ARGV.\  return -1;</span></span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      public <span class="function"><span class="keyword">function</span> <span class="title">getScript</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">      </span>&#123;  </span><br><span class="line">         <span class="keyword">return</span> &lt;&lt;&lt;LUA</span><br><span class="line">            local cmd, insert = redis.call, table.insert</span><br><span class="line">            local increment, results = ARGV[1], &#123; &#125;</span><br><span class="line"></span><br><span class="line">            for idx, key in ipairs(KEYS) </span><br><span class="line">                 if cmd('exists', key) == 1 then </span><br><span class="line">                     insert(results, idx, cmd('incrby', key, increment))</span><br><span class="line">                 else</span><br><span class="line">                     insert(results, idx, false) end</span><br><span class="line">                end</span><br><span class="line"></span><br><span class="line">            return results</span><br><span class="line">            LUA;</span><br><span class="line">       &#125;</span><br><span class="line">&#125;</span><br><span class="line">调用方式</span><br><span class="line"></span><br><span class="line">$client = new Predis\Client($single_server, array(</span><br><span class="line">    'profile' =&gt; function ($options) &#123;</span><br><span class="line">        $profile = $options-&gt;getDefault('profile');</span><br><span class="line">        $profile-&gt;defineCommand('increxby', 'IncrementExistingKeysBy');</span><br><span class="line"></span><br><span class="line">        return $profile;</span><br><span class="line">    &#125;,</span><br><span class="line">));</span><br><span class="line"></span><br><span class="line">$client-&gt;mset('foo', 10, 'foobar', 100);</span><br><span class="line"></span><br><span class="line">var_export($client-&gt;increxby('foo', 'foofoo', 'foobar', 50));</span><br><span class="line">https://learnku.com/articles/37661</span><br></pre></td></tr></table></figure>
<h3 id="Laravel-使用-Markdown-做文章编辑后台"><a href="#Laravel-使用-Markdown-做文章编辑后台" class="headerlink" title="Laravel 使用 Markdown 做文章编辑后台"></a>Laravel 使用 Markdown 做文章编辑后台</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">composer <span class="built_in">require</span> chenhua/laravel5-markdown-editor</span><br><span class="line">php artisan vendor:publish --tag=markdown</span><br><span class="line">修改 config/markdowneditor.php 配置文件</span><br><span class="line">&lt;?php</span><br><span class="line"><span class="keyword">return</span> [</span><br><span class="line">    <span class="string">"default"</span>     =&gt; <span class="string">'local'</span>, <span class="comment">//默认返回存储位置url.也可以设置七牛上传路径,阿里云存储文件等等</span></span><br><span class="line">    <span class="string">"dirver"</span>      =&gt; [<span class="string">'local'</span>], <span class="comment">//存储平台 ['local', 'qiniu', 'aliyun']</span></span><br><span class="line">    <span class="string">"connections"</span> =&gt; [</span><br><span class="line">        <span class="string">"local"</span>  =&gt; [</span><br><span class="line">            <span class="string">'prefix'</span> =&gt; <span class="string">'uploads/markdown'</span>, <span class="comment">//本地存储位置，默认uploads</span></span><br><span class="line">        ],</span><br><span class="line">        <span class="string">"qiniu"</span>  =&gt; [</span><br><span class="line">            <span class="string">'access_key'</span> =&gt; <span class="string">''</span>,</span><br><span class="line">            <span class="string">'secret_key'</span> =&gt; <span class="string">''</span>,</span><br><span class="line">            <span class="string">'bucket'</span>     =&gt; <span class="string">''</span>,</span><br><span class="line">            <span class="string">'prefix'</span>     =&gt; <span class="string">''</span>, <span class="comment">//文件前缀 file/of/path</span></span><br><span class="line">            <span class="string">'domain'</span>     =&gt; <span class="string">''</span> <span class="comment">//七牛自定义域名</span></span><br><span class="line">        ],</span><br><span class="line">        <span class="string">"aliyun"</span> =&gt; [</span><br><span class="line">            <span class="string">'ak_id'</span>     =&gt; <span class="string">''</span>,</span><br><span class="line">            <span class="string">'ak_secret'</span> =&gt; <span class="string">''</span>,</span><br><span class="line">            <span class="string">'end_point'</span>  =&gt; <span class="string">''</span>,</span><br><span class="line">            <span class="string">'bucket'</span>    =&gt; <span class="string">''</span>,</span><br><span class="line">            <span class="string">'prefix'</span>    =&gt; <span class="string">''</span>,</span><br><span class="line">        ],</span><br><span class="line">    ],</span><br><span class="line">];</span><br><span class="line"> <span class="comment">//主页显示</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">index</span>(<span class="params">Request $request</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> view(<span class="string">'index'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//提交</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">add</span>(<span class="params">Request $request</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $content = $request-&gt;input(<span class="string">'test-editormd-html-code'</span>);</span><br><span class="line">        $article=Article::create($content);</span><br><span class="line">    &#125;</span><br><span class="line">    https:<span class="comment">//learnku.com/articles/30858</span></span><br></pre></td></tr></table></figure>
<h3 id="Laravel-的测试与-PHP-Unit"><a href="#Laravel-的测试与-PHP-Unit" class="headerlink" title="Laravel 的测试与 PHP Unit"></a>Laravel 的测试与 PHP Unit</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// /tests/Feature/ExampleTest.php</span></span><br><span class="line">&lt;?php</span><br><span class="line"></span><br><span class="line">namespace Tests\Feature;</span><br><span class="line"></span><br><span class="line">use Illuminate\Foundation\Testing\RefreshDatabase;</span><br><span class="line">use Tests\TestCase;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ExampleTest</span> <span class="keyword">extends</span> <span class="title">TestCase</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * A basic test example.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @return void</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">testBasicTest</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;   </span><br><span class="line">        <span class="comment">// /vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php</span></span><br><span class="line">        $response = $<span class="keyword">this</span>-&gt;get(<span class="string">'/'</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// /vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php</span></span><br><span class="line">        $response-&gt;assertStatus(<span class="number">200</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">vendor/bin/phpunit tests/ExampleTest.php</span><br><span class="line">https:<span class="comment">//learnku.com/articles/37699</span></span><br></pre></td></tr></table></figure>
<h3 id="ORM-禁止-update-at-的自动更新"><a href="#ORM-禁止-update-at-的自动更新" class="headerlink" title="ORM 禁止 update_at 的自动更新"></a>ORM 禁止 update_at 的自动更新</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">Call to a member <span class="function"><span class="keyword">function</span> <span class="title">toDateTimeString</span>(<span class="params"></span>) <span class="title">on</span> <span class="title">string</span></span></span><br><span class="line"><span class="function"><span class="title">$user</span> = \<span class="title">App</span>\<span class="title">Models</span>\<span class="title">User</span>::<span class="title">find</span>(<span class="params"><span class="number">1</span></span>);</span></span><br><span class="line"><span class="function">  <span class="title">$user</span>-&gt;<span class="title">sex</span> = 1;</span></span><br><span class="line"><span class="function">  <span class="title">$user</span>-&gt;<span class="title">timestamps</span> = <span class="title">false</span>;</span></span><br><span class="line"><span class="function">  <span class="title">$user</span>-&gt;<span class="title">save</span>(<span class="params"></span>);</span></span><br><span class="line"><span class="function">  <span class="title">$updatedUser</span> =<span class="title">$user</span>-&gt;<span class="title">fresh</span>(<span class="params"></span>);</span></span><br><span class="line">  return [</span><br><span class="line">  <span class="string">'id'</span> =&gt; $updatedUser-&gt;id,</span><br><span class="line">  <span class="string">'name'</span> =&gt; $updatedUser-&gt;name,</span><br><span class="line">  <span class="string">'sex'</span> =&gt; $updatedUser-&gt;sex,</span><br><span class="line">  <span class="string">'created_at'</span> =&gt; $updatedUser-&gt;created_at-&gt;toDateTimeString(),</span><br><span class="line">  <span class="string">'updated_at'</span> =&gt; $updatedUser-&gt;updated_at-&gt;toDateTimeString(),</span><br><span class="line"> ];</span><br><span class="line">https:<span class="comment">//learnku.com/articles/37717</span></span><br></pre></td></tr></table></figure>
<h3 id="无限级分类"><a href="#无限级分类" class="headerlink" title="无限级分类"></a>无限级分类</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">Schema::create(<span class="string">'categories'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">Blueprint $table</span>) </span>&#123;</span><br><span class="line">    $table-&gt;increments(<span class="string">'id'</span>);</span><br><span class="line">    $table-&gt;integer(<span class="string">'parent_id'</span>);</span><br><span class="line">    $table-&gt;string(<span class="string">'code'</span>);</span><br><span class="line">    $table-&gt;string(<span class="string">'name'</span>);</span><br><span class="line">    $table-&gt;string(<span class="string">'path'</span>);</span><br><span class="line">    $table-&gt;timestamps();</span><br><span class="line">&#125;);</span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">childCategory</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;hasMany(<span class="string">'App\Models\Category'</span>, <span class="string">'parent_id'</span>, <span class="string">'id'</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">allChildrenCategorys</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;childCategory()-&gt;<span class="keyword">with</span>(<span class="string">'allChildrenCategorys'</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//无限极分类测试</span></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">index</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    $category = Category::<span class="keyword">with</span>(<span class="string">'allChildrenCategorys'</span>)-&gt;find(<span class="number">5</span>);</span><br><span class="line">    $re = $category-&gt;allChildrenCategorys;</span><br><span class="line"></span><br><span class="line">    dd($re);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//find一个parent_id = 0就可以</span></span><br><span class="line">https:<span class="comment">//learnku.com/articles/14068/simple-practice-of-laravel-infinite-class-classification</span></span><br></pre></td></tr></table></figure>
<h3 id="Laravel-在-with-查询中只查询个别字段"><a href="#Laravel-在-with-查询中只查询个别字段" class="headerlink" title="Laravel 在 with 查询中只查询个别字段"></a>Laravel 在 with 查询中只查询个别字段</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">$topics = Topic::limit(<span class="number">2</span>)-&gt;<span class="keyword">with</span>([<span class="string">'user'</span>=&gt;<span class="function"><span class="keyword">function</span>(<span class="params">$query</span>)</span>&#123;</span><br><span class="line">   $query-&gt;select(<span class="string">'id'</span>,<span class="string">'username'</span>);</span><br><span class="line">&#125;])-&gt;get();</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">BaseModel</span> <span class="keyword">extends</span> \<span class="title">Eloquent</span></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">scopeWithOnly</span>(<span class="params">$query, $relation, Array $columns</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $query-&gt;<span class="keyword">with</span>([<span class="function"><span class="params">$relation</span> =&gt;</span> <span class="function"><span class="keyword">function</span> (<span class="params">$query</span>) <span class="title">use</span> (<span class="params">$columns</span>)</span>&#123;</span><br><span class="line">            $query-&gt;select(array_merge([<span class="string">'id'</span>], $columns));</span><br><span class="line">        &#125;]);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">在我们普通的 Model 类都继承基类：</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Topic</span> <span class="keyword">extends</span> <span class="title">BaseModel</span></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">user</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;belongsTo(<span class="string">'User'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">然后使用就很方便了:https:<span class="comment">//learnku.com/laravel/t/1220/laravel-queries-only-individual-fields-in-with-q</span></span><br><span class="line"></span><br><span class="line">$topics = Topic::limit(<span class="number">2</span>)-&gt;withOnly(<span class="string">'user'</span>, [<span class="string">'username'</span>])-&gt;get();</span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">scopeWithOnly</span>(<span class="params">object $query, string $relation, array $columns</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $query-&gt;<span class="keyword">with</span>([<span class="function"><span class="params">$relation</span> =&gt;</span> <span class="function"><span class="keyword">function</span> (<span class="params">$query</span>) <span class="title">use</span> (<span class="params">$columns</span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> (count($columns) == <span class="number">1</span>) &#123;</span><br><span class="line">                $columns = array_merge([<span class="string">'id'</span>], $columns);</span><br><span class="line">            &#125;</span><br><span class="line">            $query-&gt;select($columns);</span><br><span class="line">        &#125;]);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h3 id="同步config"><a href="#同步config" class="headerlink" title="同步config"></a>同步config</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">file_put_contents(base_path() . <span class="string">'/config/cnpscy.php'</span>, <span class="string">'&lt;?php return '</span> . var_export($data_list, <span class="literal">true</span>) . <span class="string">';'</span>);</span><br><span class="line">$data_list 数组的 key =&gt; value 格式即可。</span><br><span class="line"></span><br><span class="line">我是存放 config 下面的 cnpscy</span><br><span class="line"></span><br><span class="line">读取：</span><br><span class="line"></span><br><span class="line">config(<span class="string">'cnpscy.key'</span>)</span><br><span class="line">https:<span class="comment">//learnku.com/laravel/t/38053</span></span><br></pre></td></tr></table></figure>
<h3 id="删除MySQL数据"><a href="#删除MySQL数据" class="headerlink" title="删除MySQL数据"></a>删除MySQL数据</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">$start =  time();</span><br><span class="line"><span class="keyword">while</span>(<span class="literal">true</span>) &#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                <span class="keyword">if</span>(time() - $start &gt; <span class="number">100</span>) &#123;</span><br><span class="line">                   <span class="comment">// break;</span></span><br><span class="line">                    \DB::disconnect(<span class="string">'mysql::write'</span>);<span class="comment">//断开重新连接</span></span><br><span class="line">                    $sql=<span class="string">"SET SESSION wait_timeout=65535"</span>;</span><br><span class="line">                    \DB::connection(<span class="string">'mysql::write'</span>)-&gt;select($sql);</span><br><span class="line">                    $start = time();</span><br><span class="line">                &#125;</span><br><span class="line">                \DB::connection(<span class="string">'mysql::write'</span>)-&gt;where(<span class="string">'id'</span>,<span class="string">'&gt;'</span>, <span class="number">0</span>)-&gt;<span class="keyword">delete</span>();</span><br><span class="line">            &#125;<span class="keyword">catch</span> (\Exception $e) &#123;</span><br><span class="line">                sleep(<span class="number">1</span>);</span><br><span class="line">                <span class="comment">//exit();</span></span><br><span class="line">                \DB::reconnect(<span class="string">'mysql::write'</span>);</span><br><span class="line">            &#125;</span><br></pre></td></tr></table></figure>
<h3 id="composer-支持laravel"><a href="#composer-支持laravel" class="headerlink" title="composer 支持laravel"></a>composer 支持laravel</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//github.com/shanyul/array2xml</span></span><br><span class="line">cat composer.json</span><br><span class="line"><span class="string">"extra"</span>: &#123;</span><br><span class="line">        <span class="string">"laravel"</span>: &#123;</span><br><span class="line">            <span class="string">"providers"</span>: [</span><br><span class="line">                <span class="string">"Shanyuliang\\Array2xml\\ServiceProvider"</span></span><br><span class="line">            ]</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">ServiceProvider.php</span><br><span class="line">namespace Shanyuliang\Array2xml;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ServiceProvider</span> <span class="keyword">extends</span> \<span class="title">Illuminate</span>\<span class="title">Support</span>\<span class="title">ServiceProvider</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    protected $defer = <span class="literal">true</span>;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">register</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;app-&gt;singleton(Array2xml::<span class="class"><span class="keyword">class</span>, <span class="title">function</span>()</span>&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">new</span> Array2xml();</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        $<span class="keyword">this</span>-&gt;app-&gt;alias(Array2xml::<span class="class"><span class="keyword">class</span>, '<span class="title">array2xml</span>')</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">provides</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        return [Array2xml::class, 'array2xml'];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">getXml</span>(<span class="params"></span>) </span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $arr = [</span><br><span class="line">        <span class="string">'aaa'</span> =&gt; [</span><br><span class="line">            <span class="string">'bbb'</span> =&gt; <span class="string">'ccc'</span>,</span><br><span class="line">            <span class="string">'eee'</span> =&gt; <span class="string">'fff'</span></span><br><span class="line">        ]</span><br><span class="line">    ];</span><br><span class="line">    $response = app(<span class="string">'array2xml'</span>)-&gt;generate($arr);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="Eloquent-Collection-中获-id-数组"><a href="#Eloquent-Collection-中获-id-数组" class="headerlink" title="Eloquent Collection 中获 id 数组"></a>Eloquent Collection 中获 id 数组</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 返回ID数组</span></span><br><span class="line">$permissionIDs = $role-&gt;permissions-&gt;pluck(<span class="string">'id'</span>); </span><br><span class="line"><span class="comment">//与上述结果相同。甚至相同的字符数:)</span></span><br><span class="line">$permissionIDs = $role-&gt;permissions-&gt;modelKeys();</span><br><span class="line">$allPermissions = Permission::pluck(<span class="string">'id'</span>);</span><br><span class="line">$permissions = $role-&gt;permissions-&gt;pluck(<span class="string">'id'</span>);</span><br><span class="line">$allPermissions = Permission::all()-&gt;modelKeys();</span><br><span class="line">https:<span class="comment">//learnku.com/laravel/t/39007</span></span><br></pre></td></tr></table></figure>
<h3 id="hasMany-来处理『无限极分类』"><a href="#hasMany-来处理『无限极分类』" class="headerlink" title="hasMany 来处理『无限极分类』"></a>hasMany 来处理『无限极分类』</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line">Schema::create(<span class="string">'categories'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">Blueprint $table</span>) </span>&#123;</span><br><span class="line">    $table-&gt;bigIncrements(<span class="string">'id'</span>);</span><br><span class="line">    $table-&gt;string(<span class="string">'name'</span>);</span><br><span class="line">    $table-&gt;unsignedBigInteger(<span class="string">'category_id'</span>)-&gt;nullable();</span><br><span class="line">    $table-&gt;foreign(<span class="string">'category_id'</span>)-&gt;references(<span class="string">'id'</span>)-&gt;on(<span class="string">'categories'</span>);</span><br><span class="line">    $table-&gt;timestamps();</span><br><span class="line">&#125;);</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Category</span> <span class="keyword">extends</span> <span class="title">Model</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">categories</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;hasMany(Category::<span class="class"><span class="keyword">class</span>)</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">childrenCategories</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;hasMany(Category::<span class="class"><span class="keyword">class</span>)-&gt;<span class="title">with</span>('<span class="title">categories</span>')</span>;</span><br><span class="line">&#125;</span><br><span class="line">调用 Category::<span class="keyword">with</span>(‘categories’)，将得到下级 “子分类”，但是通过 Category::<span class="keyword">with</span>(‘childrenCategories’) 将能帮你实现无限极。</span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">index</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $categories = Category::whereNull(<span class="string">'category_id'</span>)</span><br><span class="line">        -&gt;<span class="keyword">with</span>(<span class="string">'childrenCategories'</span>)</span><br><span class="line">        -&gt;get();</span><br><span class="line">    <span class="keyword">return</span> view(<span class="string">'categories'</span>, compact(<span class="string">'categories'</span>));</span><br><span class="line">&#125;</span><br><span class="line">resources/views/categories.blade.php</span><br><span class="line">&lt;ul&gt;</span><br><span class="line">    @foreach ($categories <span class="keyword">as</span> $category)</span><br><span class="line">        &lt;li&gt;&#123;&#123; $category-&gt;name &#125;&#125;&lt;<span class="regexp">/li&gt;</span></span><br><span class="line"><span class="regexp">        &lt;ul&gt;</span></span><br><span class="line"><span class="regexp">        @foreach ($category-&gt;childrenCategories as $childCategory)</span></span><br><span class="line"><span class="regexp">            @include('child_category', ['child_category' =&gt; $childCategory])</span></span><br><span class="line"><span class="regexp">        @endforeach</span></span><br><span class="line"><span class="regexp">        &lt;/u</span>l&gt;</span><br><span class="line">    @endforeach</span><br><span class="line">&lt;<span class="regexp">/ul&gt;</span></span><br><span class="line"><span class="regexp"> resources/</span>views/admin/child_category.blade.php</span><br><span class="line">&lt;li&gt;&#123;&#123; $child_category-&gt;name &#125;&#125;&lt;<span class="regexp">/li&gt;</span></span><br><span class="line"><span class="regexp">@if ($child_category-&gt;categories)</span></span><br><span class="line"><span class="regexp">    &lt;ul&gt;</span></span><br><span class="line"><span class="regexp">        @foreach ($child_category-&gt;categories as $childCategory)</span></span><br><span class="line"><span class="regexp">            @include('child_category', ['child_category' =&gt; $childCategory])</span></span><br><span class="line"><span class="regexp">        @endforeach</span></span><br><span class="line"><span class="regexp">    &lt;/u</span>l&gt;</span><br><span class="line">@endif</span><br><span class="line">https:<span class="comment">//learnku.com/laravel/t/38977</span></span><br></pre></td></tr></table></figure>
<h3 id="路由不生效"><a href="#路由不生效" class="headerlink" title="路由不生效"></a>路由不生效</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">Effect 为正常控制器补录  Effect_back 为旧的</span><br><span class="line"></span><br><span class="line">使用 find . -name <span class="string">"*.php"</span> |xargs grep -i <span class="string">"Effect_back"</span> 发现了问题所在</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">./vendor/composer/autoload_classmap.php:    <span class="string">'App\\Http\\Controllers\\Effect\\ActivityController'</span> =&gt; $baseDir . <span class="string">'/app/Http/Controllers/Effect_back/ActivityController.php'</span>,</span><br><span class="line">此问题应该是在你有新旧文件时   执行过 composer dump-autoload  所导致旧的文件自动加载进来  并进行了缓存</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">composer clear-cache</span><br><span class="line"></span><br><span class="line">composer dump-autoload</span><br><span class="line">https:<span class="comment">//learnku.com/articles/39287</span></span><br></pre></td></tr></table></figure>
<h3 id="Laravel-Eloquent"><a href="#Laravel-Eloquent" class="headerlink" title="Laravel Eloquent"></a>Laravel Eloquent</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">$user = User::where(<span class="string">'name'</span>, <span class="string">'wuqinqiang'</span>)-&gt;first();</span><br><span class="line">$user-&gt;age = <span class="number">18</span>;</span><br><span class="line">$user-&gt;wechat-&gt;nickName = <span class="string">"Remember"</span>;</span><br><span class="line">$user-&gt;save()</span><br><span class="line">那么此时，会保存用户信息，但是并不会保存对应 Wechat 的关联信息，想让它一起保存咋么办？也很简单。把 save 换成 push 即可。</span><br><span class="line"></span><br><span class="line">$user-&gt;push();</span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">boot</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        parent::boot();</span><br><span class="line">        <span class="keyword">static</span>::creating(<span class="function"><span class="keyword">function</span> (<span class="params">$model</span>) </span>&#123;</span><br><span class="line">           <span class="comment">//业务逻辑</span></span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">那要是不想在操作时触发任何事件则么办？你可以执行一个回调函数而无需触发任何事件:</span><br><span class="line"></span><br><span class="line">$user = User::withoutEvents(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line"><span class="keyword">return</span> <span class="comment">//保存或者更新代码</span></span><br><span class="line">&#125;);</span><br><span class="line">    </span><br><span class="line">    $ user = User :: where(<span class="string">'name'</span>,<span class="string">'wuqinqiang'</span>)-&gt;first();</span><br><span class="line">    $ user-&gt; isDirty（）; <span class="comment">// 还没被动过 返回 false</span></span><br><span class="line">    $ user-&gt; name =<span class="string">'curry'</span>;  </span><br><span class="line">    $ user-&gt; isDirty（）; <span class="comment">//模型被动过了 返回true </span></span><br><span class="line">    $ user-&gt; isDirty（<span class="string">'email'</span>）; <span class="comment">// 可以传入参数  验证某个参数是否被动过，这个我还没碰过 当然 false</span></span><br><span class="line">    $ user-&gt; isDirty（<span class="string">'name'</span>）; <span class="comment">// true </span></span><br><span class="line">    如果你还想具体了解到哪些是被动过了，可以使用:</span><br><span class="line">    </span><br><span class="line">    $user-&gt;getDirty();  你将得到一个数组 [<span class="string">"name"</span> =&gt; <span class="string">"curry"</span>]</span><br><span class="line">    $ user = User :: where(<span class="string">'name'</span>,<span class="string">'wuqinqiang'</span>)-&gt;first();</span><br><span class="line">    $user-&gt;name = <span class="string">'curry'</span>;</span><br><span class="line">    $user-&gt;getOriginal(); <span class="comment">// 返回数组 "name" =&gt; "wuqinqiang"</span></span><br><span class="line">    $user-&gt;getOriginal(<span class="string">'name'</span>); <span class="comment">//指定要看哪个属性的原值  "wuqinqiang"</span></span><br><span class="line">    https:<span class="comment">//learnku.com/articles/39263</span></span><br></pre></td></tr></table></figure>
<h3 id="自定义-Artisan-命令"><a href="#自定义-Artisan-命令" class="headerlink" title="自定义 Artisan 命令"></a>自定义 Artisan 命令</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// routes/console.php</span></span><br><span class="line">Artisan::command(<span class="string">'password:reset &#123;userId&#125; &#123;--sendEmail&#125;'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="comment">// 第一个参数是命令的名称</span></span><br><span class="line">    <span class="comment">// 执行一些操作 ，比如重置用户密码 ...</span></span><br><span class="line">&#125;)</span><br><span class="line">protected $signature = <span class="string">'password:reset &#123;userId&#125; &#123;--sendEmail&#125;'</span>;</span><br><span class="line"> <span class="comment">// 定义 ：password:reset &#123;userId&#125;</span></span><br><span class="line">    <span class="comment">// 执行 ：php artisan password:reset 5</span></span><br><span class="line"></span><br><span class="line">    $<span class="keyword">this</span>-&gt;argument(); <span class="comment">// 返回一个包含所有参数的数组（ 第一个元素是命令名称 ）</span></span><br><span class="line">    <span class="comment">// [</span></span><br><span class="line">    <span class="comment">//     "command" =&gt; "password:reset",</span></span><br><span class="line">    <span class="comment">//     "userId" =&gt; "5"</span></span><br><span class="line">    <span class="comment">// ]</span></span><br><span class="line"></span><br><span class="line">    $<span class="keyword">this</span>-&gt;argument(<span class="string">'userId'</span>);</span><br><span class="line">    <span class="comment">// "5"</span></span><br><span class="line"><span class="comment">// 定义 ：password:reset &#123;--userId=&#125;</span></span><br><span class="line">    <span class="comment">// 执行 ：php artisan password:reset --userId=5</span></span><br><span class="line"></span><br><span class="line">    $<span class="keyword">this</span>-&gt;option(); <span class="comment">// 返回一个包含所有选项的数组</span></span><br><span class="line">    <span class="comment">// [</span></span><br><span class="line">    <span class="comment">//     "userId" =&gt; "5",</span></span><br><span class="line">    <span class="comment">//     "help" =&gt; false,</span></span><br><span class="line">    <span class="comment">//     "quiet" =&gt; false,</span></span><br><span class="line">    <span class="comment">//     "verbose" =&gt; false,</span></span><br><span class="line">    <span class="comment">//     "version" =&gt; false,</span></span><br><span class="line">    <span class="comment">//     "ansi" =&gt; false,</span></span><br><span class="line">    <span class="comment">//     "no-interaction" =&gt; false,</span></span><br><span class="line">    <span class="comment">//     "env" =&gt; null</span></span><br><span class="line">    <span class="comment">// ]</span></span><br><span class="line">    $<span class="keyword">this</span>-&gt;option(<span class="string">'userId'</span>);</span><br><span class="line">    <span class="comment">// "5"</span></span><br><span class="line"><span class="comment">// 提示用户输入</span></span><br><span class="line">$email = $<span class="keyword">this</span>-&gt;ask(<span class="string">"你的邮箱是什么？"</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 提示用户输入 ，但用 "*" 隐藏输入内容</span></span><br><span class="line">$password = $<span class="keyword">this</span>-&gt;secret(<span class="string">"你的数据库密码是什么？"</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 提示用户输入是 / 否 ，返回布尔值 ，除了按 y 和 Y ，其他的输入都会返回 false</span></span><br><span class="line"><span class="keyword">if</span> ($<span class="keyword">this</span>-&gt;confirm(<span class="string">"你确定要重置用户的密码？"</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">// 提示用户选择选项 ，如果用户没有选择 ，默认值就是最后一个选项</span></span><br><span class="line">$tips = <span class="string">'你希望在每天什么时间备份数据库？'</span></span><br><span class="line">$options = array(<span class="string">"08:00"</span>, <span class="string">"20:00"</span>, <span class="string">"12:00"</span>);</span><br><span class="line">$<span class="keyword">default</span> = <span class="number">0</span>;</span><br><span class="line">$index = $<span class="keyword">this</span>-&gt;choice($tips, $options, $<span class="keyword">default</span>);</span><br><span class="line"><span class="comment">// 这里注意返回的是用户选择的 key ，而不是 value</span></span><br><span class="line">print_r($optios[$index]);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 还可以使用关联数组</span></span><br><span class="line">$tips = <span class="string">'你现在有两个选择'</span>;</span><br><span class="line">$options = array(<span class="string">"a"</span> =&gt; <span class="string">"自己脱"</span>, <span class="string">"b"</span> =&gt; <span class="string">"我帮你脱"</span>);</span><br><span class="line">$<span class="keyword">default</span> = <span class="string">"b"</span>;</span><br><span class="line">$index = $<span class="keyword">this</span>-&gt;choice($tips, $options, $<span class="keyword">default</span>);</span><br><span class="line">print_r($optios[$index]);</span><br><span class="line">$headers = array(<span class="string">'name'</span>, <span class="string">'email'</span>);</span><br><span class="line">$data = array(</span><br><span class="line">    array(<span class="string">'马云'</span>, <span class="string">'jack_ma@aliyun.com'</span>),</span><br><span class="line">    array(<span class="string">'马化腾'</span>, <span class="string">'pony@qq.com'</span>)</span><br><span class="line">);</span><br><span class="line"><span class="comment">// $data = App\User::all(['name', 'email'])-&gt;toArray();</span></span><br><span class="line">$<span class="keyword">this</span>-&gt;table($headers, $data);</span><br><span class="line"><span class="comment">// 将进度条分为 10 份</span></span><br><span class="line">$total = <span class="number">10</span>;</span><br><span class="line"><span class="comment">// 创建进度条</span></span><br><span class="line">$<span class="keyword">this</span>-&gt;output-&gt;progressStart($total);</span><br><span class="line"><span class="comment">// 循环</span></span><br><span class="line"><span class="keyword">for</span> ($i = <span class="number">0</span>; $i &lt; $total; $i++) &#123;</span><br><span class="line">    sleep(<span class="number">1</span>);</span><br><span class="line">    <span class="comment">// 进度条步进</span></span><br><span class="line">    $<span class="keyword">this</span>-&gt;output-&gt;progressAdvance();</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 结束进度条</span></span><br><span class="line">$<span class="keyword">this</span>-&gt;output-&gt;progressFinish();</span><br><span class="line"># 2</span><br><span class="line"><span class="number">2</span>/<span class="number">10</span> [=====&gt;----------------------]  <span class="number">20</span>%</span><br><span class="line"># 10</span><br><span class="line"><span class="number">10</span>/<span class="number">10</span> [============================] <span class="number">100</span>%</span><br><span class="line"><span class="comment">// Artisan::call()</span></span><br><span class="line">Route::get(<span class="string">'test-artisan'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123; </span><br><span class="line">    $exitCode = Artisan::call(<span class="string">"password:reset"</span>, [</span><br><span class="line">        <span class="string">'userId'</span> =&gt; <span class="number">15</span>, <span class="string">'--sendEmail'</span> =&gt; <span class="literal">true</span></span><br><span class="line">    ])</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// $this-&gt;call() 或者 $this-&gt;callSilent()</span></span><br><span class="line">Route::get(<span class="string">'test-artisan'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123; </span><br><span class="line">    <span class="comment">// 这个 callSilent() 有什么区别我也不太懂</span></span><br><span class="line">    $exitCode = $<span class="keyword">this</span>-&gt;callSilent(<span class="string">"password:reset"</span>, [</span><br><span class="line">        <span class="string">'userId'</span> =&gt; <span class="number">15</span>, <span class="string">'--sendEmail'</span> =&gt; <span class="literal">true</span></span><br><span class="line">    ])</span><br><span class="line">&#125;)https:<span class="comment">//learnku.com/articles/39394</span></span><br></pre></td></tr></table></figure>
<h3 id="递归子级"><a href="#递归子级" class="headerlink" title="递归子级"></a>递归子级</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">在 model 里面增加子级关联</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">child</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;hasMany(self::<span class="class"><span class="keyword">class</span>,'<span class="title">pid</span>')</span>;</span><br><span class="line">    &#125;</span><br><span class="line">在 model 里面增加子级递归关联</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">childRecursive</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;child()-&gt;<span class="keyword">with</span>(<span class="string">'childRecursive'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">调用的时候建议用 <span class="keyword">with</span> 方法预加载https:<span class="comment">//learnku.com/articles/39687</span></span><br><span class="line"></span><br><span class="line">    $menu = Menu::<span class="keyword">with</span>(<span class="string">'childRecursive'</span>)-&gt;find($id);</span><br></pre></td></tr></table></figure>
<p><a href="http://www.siya89.com/blog/laravel%205.5%20blog%20eighth" target="_blank" rel="noopener">laravel5.5 博客第八篇</a></p>
<p><a href="https://learnku.com/articles/39723" target="_blank" rel="noopener">多商户laravel vue开源商城 </a></p>
<p><a href="https://github.com/guojiangclub/ecommerce-open-api" target="_blank" rel="noopener">基于 Laravel + swoole + 小程序的开源电商系统</a></p>
<p><a href="https://learnku.com/laravel/t/39571" target="_blank" rel="noopener">Laravel API 错误处理</a></p>
<p><a href="https://github.com/cranux/LaravelDevTemplate" target="_blank" rel="noopener">laravel + swoole + adminlte 高效开发模板</a></p>
<p><a href="https://learnku.com/articles/38969" target="_blank" rel="noopener">Laravel 项目 伪静态分页处理</a></p>
<p><a href="https://learnku.com/articles/21372" target="_blank" rel="noopener">更快的找到自己所需的模型关联类型</a></p>
<p><a href="https://www.shanyuliang.com/archives/246" target="_blank" rel="noopener">Laravel-dompdf优雅导出PDF</a></p>
<p><a href="https://github.com/shanyul/photosLive" target="_blank" rel="noopener">Laravel + Vue + Oss 搭建的图片墙小站</a></p>
<p><a href="https://learnku.com/articles/38067" target="_blank" rel="noopener">Laravel editor.md 支持截图 / 粘贴上传图片</a></p>
<p><a href="https://www.shanyuliang.com/archives/286" target="_blank" rel="noopener"> ConsoleTVs/Charts 简单快速绘制统计图</a></p>
<p><a href="https://learnku.com/laravel/t/38446" target="_blank" rel="noopener">91 个常见的 Laravel 面试题和答案</a></p>
<p><a href="http://sakyavarro.cn/post/%E8%AE%B0%E5%BD%95%E5%B0%86%E4%B8%80%E4%B8%AA%20Laravel%205.2%20%E9%A1%B9%E7%9B%AE%E5%8D%87%E7%BA%A7%E8%87%B3%205.8%20%E7%9A%84%E8%BF%87%E7%A8%8B" target="_blank" rel="noopener"> Laravel 5.2 项目升级至 5.8 的过程</a></p>
<p><a href="https://learnku.com/articles/37478" target="_blank" rel="noopener"> Laravel 中实现「福勒的货币设计模式」</a></p>
<p><a href="https://gitee.com/uknowfantasy/FantasyCode.git" target="_blank" rel="noopener">Laravel6.0 框架和 semantic-ui 开发的一个个人博客社区网站</a></p>
<p><a href="https://leoyang90.gitbooks.io/laravel-source-analysis/content/" target="_blank" rel="noopener">laravel 源码详解</a></p>
<p><a href="https://github.com/xiaohuilam/searching" target="_blank" rel="noopener">仿 Laravel-china 的搜索包</a></p>
<p><a href="https://learnku.com/articles/37550" target="_blank" rel="noopener">Laravel 5.5 + H+UI 框架的权限管理后台</a></p>
<p><a href="https://learnku.com/articles/36012" target="_blank" rel="noopener">Laravel 服务容器</a></p>
<p><a href="https://github.com/pudongping/larablog" target="_blank" rel="noopener">博客支持 Markdown,支持 RBAC 权限管理</a></p>
<p><a href="https://learnku.com/articles/30982" target="_blank" rel="noopener">三分钟从零部署 Laravel 应用到阿里云 ECS</a></p>
<p><a href="https://learnku.com/articles/37459" target="_blank" rel="noopener">Laravel+easywechat 实现公众号微信支付</a></p>
<p><a href="https://learnku.com/articles/37163" target="_blank" rel="noopener">Laravel-Gii 可视化代码生成工具</a></p>
<p><a href="https://gitee.com/neilyoz/laravel-swoole-chat" target="_blank" rel="noopener">Laravel + Vue + Swoole 实现的简单 Web 聊天</a></p>
<p><a href="https://gitee.com/obamajs/php-base-container" target="_blank" rel="noopener">laravel容器</a></p>
<p><a href="https://learnku.com/articles/35675" target="_blank" rel="noopener">Laravel MongoDB 数据库查询</a></p>
<p><a href="https://learnku.com/articles/34959" target="_blank" rel="noopener"> Laravel 框架 5.1 升级到 5.5 </a></p>
<p><a href="https://github.com/viest/php-ext-excel-export" target="_blank" rel="noopener">php excel</a></p>
<p><a href="https://learnku.com/courses/laravel-performance/6.x" target="_blank" rel="noopener">Laravel 性能优化入门</a></p>
<p><a href="https://learnku.com/articles/36843" target="_blank" rel="noopener">安装 laradock 以及运行 Laravel 项目</a></p>
<p><a href="https://github.com/FireLustre/php-dfa-sensitive" target="_blank" rel="noopener">过滤敏感词汇扩展</a></p>
<p><a href="https://learnku.com/articles/29049#871bbd" target="_blank" rel="noopener">Windows 环境下安装 Laravel</a></p>
<p><a href="https://github.com/moell-peng/laravel-layui-admin" target="_blank" rel="noopener">Laravel-layui-admin : 支持 Laravel6.0 的 RBAC 后台系统</a></p>
<p><a href="https://github.com/telanflow/laravel-binlog" target="_blank" rel="noopener">Laravel-Binlog 扩展</a></p>
<p><a href="https://learnku.com/articles/36574" target="_blank" rel="noopener"> Laravel 自带消息模块搭建小程序实时推送消息</a></p>
<p><a href="https://github.com/shulidata/easyalipay" target="_blank" rel="noopener">基于支付宝 OpenAPI 开源的 PHP 语言版本 SDK</a></p>
<p><a href="https://www.gitbook.com/book/leoyang90/laravel-source-analysis" target="_blank" rel="noopener">gitbook 的 Laravel 源码详解</a></p>
<p><a href="https://cdn.learnku.com/uploads/images/201911/15/39249/so44qFVX1y.png!/fw/1240" target="_blank" rel="noopener">Application 类 make 方法调用栈流程图</a></p>
<p><a href="https://github.com/kong-qi/kongqi_laravel_admin" target="_blank" rel="noopener">快速laravel后台管理系统</a></p>
<p><a href="https://gitee.com/obamajs/php-websocket-base-implemention" target="_blank" rel="noopener">PHP 实现 Websocket 协议</a></p>
<p><a href="https://learnku.com/articles/35992" target="_blank" rel="noopener">Laravel-S 项目之初体验</a></p>
<p><a href="https://github.com/tsmliyun/laravel_quick_admin" target="_blank" rel="noopener">基于laravel5.5 + H-ui开发的一款基础后台管理系统</a></p>
<p><a href="https://github.com/kong-qi/kongqi_laravel_admin" target="_blank" rel="noopener">快速laravel后台管理系统</a></p>
<p><a href="https://github.com/imnotdoubi/laravel-admin.git" target="_blank" rel="noopener">Laravel-admin 码的电子商务前后端系统</a></p>
<p><a href="https://learnku.com/laravel/t/32401" target="_blank" rel="noopener">Laravel+vue 个人博客https://github.com/sweida/laravel-blog-api/</a></p>
<p><a href="https://github.com/bobo1576554465/laravel-source-analysis" target="_blank" rel="noopener">详解 laravel 源码 https://leoyang90.gitbooks.io/laravel-source-analysis/content/</a></p>
<p><a href="https://www.kancloud.cn/martist/ma_zhao_liu/370443" target="_blank" rel="noopener">web开发者知识体系电子书</a></p>
<p><a href="https://learnku.com/articles/35672" target="_blank" rel="noopener"> EasyWechat 和 Laravel notification 发送微信小程序模板消息</a></p>
<p><a href="https://learnku.com/articles/35747" target="_blank" rel="noopener">QQ 登录、微信登录、微博登录、GitHub 登录</a></p>
<p><a href="https://learnku.com/articles/35644" target="_blank" rel="noopener">快速定位无用路由</a></p>
<p><a href="https://learnku.com/laravel/t/35638" target="_blank" rel="noopener">Laravel 上将图片上传到阿里云 OSS</a></p>
<p><a href="https://learnku.com/articles/35592" target="_blank" rel="noopener">快速记忆php系统数组函数</a></p>
<p><a href="https://learnku.com/laravel/t/35561" target="_blank" rel="noopener">附近的店铺</a></p>
<p><a href="https://github.com/snower/forsun-laravel" target="_blank" rel="noopener">高性能的定时调度服务Forsun的Laravel组件</a></p>
<p><a href="https://www.hongfs.cn/2019/06/php/laravel/laravel-query-nearby-data/" target="_blank" rel="noopener">Laravel 查询附近的数据</a></p>
<p><a href="https://github.com/medcl/elasticsearch-rtf" target="_blank" rel="noopener">elasticsearch中文发行版</a></p>
<p><a href="https://www.hongfs.cn/2018/12/php/laravel/laravel-multi-level-linkage/" target="_blank" rel="noopener">多级联动</a></p>
<p><a href="https://learnku.com/articles/30812" target="_blank" rel="noopener">Laravel 下 Elasticsearch/Algolia 全文搜索</a></p>
<p><a href="https://learnku.com/articles/35377" target="_blank" rel="noopener">Phpstorm 开启 Laravel 代码提示</a></p>
<p><a href="https://github.com/shockerli/php-awesome" target="_blank" rel="noopener">PHP 最优秀资源的整理汇集</a></p>
<p><a href="https://learnku.com/articles/35348" target="_blank" rel="noopener">腾讯地图搜索</a></p>
<p><a href="https://learnku.com/articles/34959" target="_blank" rel="noopener">Laravel 框架 5.1 升级到 5.5</a></p>
<p><a href="https://learnku.com/articles/35101" target="_blank" rel="noopener">Laravel Authorization：支持 ACL、RBAC、ABAC 等模型的授权库</a></p>
<p><a href="https://learnku.com/laravel/t/35080" target="_blank" rel="noopener">PSR-12 编码规范扩充</a></p>
<p><a href="https://learnku.com/articles/30812" target="_blank" rel="noopener">Laravel 下 Elasticsearch/Algolia 全文搜索 使用案例</a></p>
<p> <a href="https://learnku.com/articles/6207/lightweight-full-text-retrieval-engine-tntsearch-and-chinese-word-segmentation" target="_blank" rel="noopener">轻量级全文检索引擎 TNTSearch 和中文分词</a></p>
<p><a href="https://baijunyao.com/article/154" target="_blank" rel="noopener">laravel下TNTSearch+jieba-php实现中文全文搜索</a></p>
<p><a href="http://www.putyy.com/article/32" target="_blank" rel="noopener">postman使用记录</a></p>
<p><a href="https://github.com/jianyan74/rageframe2" target="_blank" rel="noopener">免费开源的在线文档管理插件</a></p>
<p><a href="https://gitee.com/leiyong3/laravel_blog" target="_blank" rel="noopener">诗词博客</a></p>
<p><a href="https://thans.cn/mirror/elasticsearch.html" target="_blank" rel="noopener">Elasticsearch 国内镜像下载站</a></p>
<p><a href="https://github.com/ChinaBygones/PHP-DouyinRobot" target="_blank" rel="noopener">PHP抖音机器人</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/10/15/那些在国内还能使用的谷歌产品/" rel="next" title="那些在国内还能使用的谷歌产品">
                <i class="fa fa-chevron-left"></i> 那些在国内还能使用的谷歌产品
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/10/22/2019-年如何优雅的上谷歌/" rel="prev" title="2019 年如何优雅的上谷歌">
                2019 年如何优雅的上谷歌 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
          <div class="comments" id="comments">
             <div id="gitment-container"></div>
         </div>
  




        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">苏生不惑</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">382</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">37</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/sushengbuhuo" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mysusheng@gmail.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://plus.google.com/mysusheng" target="_blank" title="Google">
                      
                        <i class="fa fa-fw fa-google"></i>Google</a>
                  </span>
                
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                Links
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="http://v2ex.com/" title="v2ex" target="_blank">v2ex</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.fanhaobai.com/" title="fanhaobai" target="_blank">fanhaobai</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://yuanxuxu.com/archives/" title="yuanxuxu" target="_blank">yuanxuxu</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.snail-c.cn/article" title="snail-c" target="_blank">snail-c</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://showcj.com/archives" title="showcj" target="_blank">showcj</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://vultr.aicnm.com/%E6%9C%80%E6%96%B0Vultr%E6%B3%A8%E5%86%8C%E5%8F%8AVPS%E8%B4%AD%E4%B9%B0%E5%9B%BE%E6%96%87%E6%95%99%E7%A8%8B/" title="vultr" target="_blank">vultr</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.lucissfer.com/" title="lucissfer" target="_blank">lucissfer</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.csdn.net/fdipzone/article/details/79352685" title="傲雪星枫" target="_blank">傲雪星枫</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.yoby123.cn/index.php/category/default/" title="小白的分享" target="_blank">小白的分享</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.cnblogs.com/52fhy/p/5819995.html" title="PHP攻城狮" target="_blank">PHP攻城狮</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.jiaojie.site/" title="php" target="_blank">php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://sphard.com/archives/" title="sphard" target="_blank">sphard</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://yuanxuxu.com/archives/" title="LNMP技术栈笔记" target="_blank">LNMP技术栈笔记</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.coding10.com/" title="学习 Laravel" target="_blank">学习 Laravel</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://shuwoom.com/?page_id=929" title="区块链学习指南" target="_blank">区块链学习指南</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://greenlightt.github.io/archives/" title="greenlightt" target="_blank">greenlightt</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.0php.net/archives/" title="0php" target="_blank">0php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.fordba.com/category/mysql" title="mysql" target="_blank">mysql</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.itcodemonkey.com/" title="程序员" target="_blank">程序员</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.yanshuo.me/r/v2ex" title="言说" target="_blank">言说</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.timiguo.com/archive.html" title="提米果的博客" target="_blank">提米果的博客</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://phpartisan.cn/news/112.html" title="phpartisan" target="_blank">phpartisan</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.cnblogs.com/52fhy/" title="飞鸿影" target="_blank">飞鸿影</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.54php.cn/" title="54php" target="_blank">54php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.lazyman.vip/" title="营销" target="_blank">营销</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.njphper.com/archives/" title="做人呢最重要的就是开心" target="_blank">做人呢最重要的就是开心</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.h57.pw/" title="php 初心者" target="_blank">php 初心者</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-网站不会被嵌入到其他站点中"><span class="nav-number">1.</span> <span class="nav-text">Laravel 网站不会被嵌入到其他站点中</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Uncaught-ReflectionException-Class-request-does-not-exist"><span class="nav-number">2.</span> <span class="nav-text">Uncaught ReflectionException: Class request does not exist</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-下-TNTSearch-jieba-PHP-实现中文全文搜索"><span class="nav-number">3.</span> <span class="nav-text">Laravel 下 TNTSearch+jieba-PHP 实现中文全文搜索</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#二分查找"><span class="nav-number">4.</span> <span class="nav-text">二分查找</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#子孙树"><span class="nav-number">5.</span> <span class="nav-text">子孙树</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-的设计哲学"><span class="nav-number">6.</span> <span class="nav-text">Laravel 的设计哲学</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#高并发业务场景下的秒杀解决方案"><span class="nav-number">7.</span> <span class="nav-text">高并发业务场景下的秒杀解决方案</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP-开启-Opcache-后如何优雅地部署-PHP-代码"><span class="nav-number">8.</span> <span class="nav-text">PHP 开启 Opcache 后如何优雅地部署 PHP 代码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#模型-query-无数据"><span class="nav-number">9.</span> <span class="nav-text">模型::query () 无数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#JSON-encode-小数位丢失"><span class="nav-number">10.</span> <span class="nav-text">JSON_encode 小数位丢失</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#支付宝公钥证书-PHP-版本-SDK"><span class="nav-number">11.</span> <span class="nav-text">支付宝公钥证书 PHP 版本 SDK</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#字符串表达式计算"><span class="nav-number">12.</span> <span class="nav-text">字符串表达式计算</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#用户登录密码改为-md5"><span class="nav-number">13.</span> <span class="nav-text">用户登录密码改为 md5</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel跨库跨连接的事务操作"><span class="nav-number">14.</span> <span class="nav-text">Laravel跨库跨连接的事务操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#array-splice无法自定义键值"><span class="nav-number">15.</span> <span class="nav-text">array_splice无法自定义键值</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#数字转度量"><span class="nav-number">16.</span> <span class="nav-text">数字转度量</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#获取两个坐标之间距离"><span class="nav-number">17.</span> <span class="nav-text">获取两个坐标之间距离</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-上传图片"><span class="nav-number">18.</span> <span class="nav-text">Laravel 上传图片</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#拼音排序"><span class="nav-number">19.</span> <span class="nav-text">拼音排序</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP读取微信客户端数据库"><span class="nav-number">20.</span> <span class="nav-text">PHP读取微信客户端数据库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP7-4-可能会对-Laravel-生态带来的改变"><span class="nav-number">21.</span> <span class="nav-text">PHP7.4 可能会对 Laravel 生态带来的改变</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#无限极分类"><span class="nav-number">22.</span> <span class="nav-text">无限极分类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#限制请求频率中间件"><span class="nav-number">23.</span> <span class="nav-text">限制请求频率中间件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-MongoDB-数据库查询"><span class="nav-number">24.</span> <span class="nav-text">Laravel MongoDB 数据库查询</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#树状数据结构存储方式"><span class="nav-number">25.</span> <span class="nav-text">树状数据结构存储方式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#冬令时"><span class="nav-number">26.</span> <span class="nav-text">冬令时</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#展开表达式"><span class="nav-number">27.</span> <span class="nav-text">展开表达式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#helper函数"><span class="nav-number">28.</span> <span class="nav-text">helper函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#魔术方法-call"><span class="nav-number">29.</span> <span class="nav-text">魔术方法__call</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#获取每年所有周"><span class="nav-number">30.</span> <span class="nav-text">获取每年所有周</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#php-yield"><span class="nav-number">31.</span> <span class="nav-text">php yield</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP-规范-Symfony-代码规范"><span class="nav-number">32.</span> <span class="nav-text">PHP 规范 - Symfony 代码规范</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-限制条数后再分页"><span class="nav-number">33.</span> <span class="nav-text">Laravel 限制条数后再分页</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#php-ini-配置"><span class="nav-number">34.</span> <span class="nav-text">php.ini 配置</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP-Traits"><span class="nav-number">35.</span> <span class="nav-text">PHP Traits</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#正则表达式"><span class="nav-number">36.</span> <span class="nav-text">正则表达式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#curl-下载文件"><span class="nav-number">37.</span> <span class="nav-text">curl 下载文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#更新数据后返回原来数据"><span class="nav-number">38.</span> <span class="nav-text">更新数据后返回原来数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#生成器"><span class="nav-number">39.</span> <span class="nav-text">生成器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#多维数组变一维"><span class="nav-number">40.</span> <span class="nav-text">多维数组变一维</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#phpexcel遇到内存溢出"><span class="nav-number">41.</span> <span class="nav-text">phpexcel遇到内存溢出</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#基于雪花算法的-PHP-ID-生成器"><span class="nav-number">42.</span> <span class="nav-text">基于雪花算法的 PHP ID 生成器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Nginx-与-PHP-的通信"><span class="nav-number">43.</span> <span class="nav-text">Nginx 与 PHP 的通信</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Elasticsearch-Algolia-全文搜索"><span class="nav-number">44.</span> <span class="nav-text">Elasticsearch/Algolia 全文搜索</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP-系统调用time"><span class="nav-number">45.</span> <span class="nav-text">PHP 系统调用time()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#curl-下载进度"><span class="nav-number">46.</span> <span class="nav-text">curl 下载进度</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#队列执行频率限制"><span class="nav-number">47.</span> <span class="nav-text">队列执行频率限制</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#上传文件和下载"><span class="nav-number">48.</span> <span class="nav-text">上传文件和下载</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#app-接口对接"><span class="nav-number">49.</span> <span class="nav-text">app 接口对接</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#异常类"><span class="nav-number">50.</span> <span class="nav-text">异常类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#where-or-查询"><span class="nav-number">51.</span> <span class="nav-text">where or 查询</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#查询封装"><span class="nav-number">52.</span> <span class="nav-text">查询封装</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#redis同步到数据库"><span class="nav-number">53.</span> <span class="nav-text">redis同步到数据库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#where-in"><span class="nav-number">54.</span> <span class="nav-text">where in</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#付费文章试读"><span class="nav-number">55.</span> <span class="nav-text">付费文章试读</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#全局记录管理员的所有操作"><span class="nav-number">56.</span> <span class="nav-text">全局记录管理员的所有操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Column-not-found-1054-Unknown-column"><span class="nav-number">57.</span> <span class="nav-text">Column not found: 1054 Unknown column</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#groupBy-分组查询的分页"><span class="nav-number">58.</span> <span class="nav-text">groupBy 分组查询的分页</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#php-artisan-tinker编码"><span class="nav-number">59.</span> <span class="nav-text">php artisan tinker编码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#api文档l5-swagger"><span class="nav-number">60.</span> <span class="nav-text">api文档l5-swagger</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-里面的-chunk-分块效率"><span class="nav-number">61.</span> <span class="nav-text">Laravel 里面的 chunk 分块效率</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Facade-demo"><span class="nav-number">62.</span> <span class="nav-text">Facade demo</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#验证码（Captcha）包乱码"><span class="nav-number">63.</span> <span class="nav-text">验证码（Captcha）包乱码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#laravel权限设计"><span class="nav-number">64.</span> <span class="nav-text">laravel权限设计</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#路由多版本拆分"><span class="nav-number">65.</span> <span class="nav-text">路由多版本拆分</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#lua-脚本-运行-Redis-命令"><span class="nav-number">66.</span> <span class="nav-text">lua 脚本 运行 Redis 命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-使用-Markdown-做文章编辑后台"><span class="nav-number">67.</span> <span class="nav-text">Laravel 使用 Markdown 做文章编辑后台</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-的测试与-PHP-Unit"><span class="nav-number">68.</span> <span class="nav-text">Laravel 的测试与 PHP Unit</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#ORM-禁止-update-at-的自动更新"><span class="nav-number">69.</span> <span class="nav-text">ORM 禁止 update_at 的自动更新</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#无限级分类"><span class="nav-number">70.</span> <span class="nav-text">无限级分类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-在-with-查询中只查询个别字段"><span class="nav-number">71.</span> <span class="nav-text">Laravel 在 with 查询中只查询个别字段</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#同步config"><span class="nav-number">72.</span> <span class="nav-text">同步config</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#删除MySQL数据"><span class="nav-number">73.</span> <span class="nav-text">删除MySQL数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#composer-支持laravel"><span class="nav-number">74.</span> <span class="nav-text">composer 支持laravel</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Eloquent-Collection-中获-id-数组"><span class="nav-number">75.</span> <span class="nav-text">Eloquent Collection 中获 id 数组</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#hasMany-来处理『无限极分类』"><span class="nav-number">76.</span> <span class="nav-text">hasMany 来处理『无限极分类』</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#路由不生效"><span class="nav-number">77.</span> <span class="nav-text">路由不生效</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-Eloquent"><span class="nav-number">78.</span> <span class="nav-text">Laravel Eloquent</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#自定义-Artisan-命令"><span class="nav-number">79.</span> <span class="nav-text">自定义 Artisan 命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#递归子级"><span class="nav-number">80.</span> <span class="nav-text">递归子级</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">苏生不惑</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>



<div>
<span id="showDays"></span>

</div>

<span id="busuanzi_container_site_pv">
   总访问量:<span id="busuanzi_value_site_pv"></span>次
</span>



  <span class="post-meta-divider">|</span>



<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共773.8k字</span>
</div>
<script>
var birthDay = new Date("11/20/2018");
var now = new Date();
var duration = now.getTime() - birthDay.getTime();
var total= Math.floor(duration / (1000 * 60 * 60 * 24));
document.getElementById("showDays").innerHTML = "本站已运行 "+total+" 天";
</script>

        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      人次
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  











<script type="text/javascript">
    (function() {
        // 匿名函数，防止污染全局变量
        var utterances = document.createElement('script');
        utterances.type = 'text/javascript';
        utterances.async = true;
        utterances.setAttribute('issue-term','0')
        utterances.setAttribute('theme','')
        utterances.setAttribute('repo','sushengbuhuo/laravel_ioc_demo')
        utterances.crossorigin = 'anonymous';
        utterances.src = 'https://utteranc.es/client.js';
        // content 是要插入评论的地方
        document.getElementById('gitment-container').appendChild(utterances);
    })();
</script>


  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

  
<script>
  ((window.gitter = {}).chat = {}).options = {
    //room替换成自己的聊天室名称即可，room的名称规则是：username/roomname
    room: 'sushengbuhuo-chat/mychat'
  };
</script>
<script src="https://sidecar.gitter.im/dist/sidecar.v1.js" async defer></script>

</body>
</html>
